java - Stuck in "Concurrency In Practise" article no Listing 2.1. A Stateless Servlet -
from book:
2.1.1. example: stateless servlet in chapter 1, listed number of frameworks create threads , call components threads, leaving responsibility of making components thread-safe. often, thread-safety requirements stem not decision use threads directly decision use facility servlets framework. we're going develop simple examplea servlet-based factorization serviceand extend add features while preserving thread safety.
listing 2.1 shows our simple factorization servlet. unpacks number factored servlet request, factors it, , packages results servlet response.
and code example
@threadsafe public class statelessfactorizer implements servlet { public void service(servletrequest req, servletresponse resp) { biginteger = extractfromrequest(req); biginteger[] factors = factor(i); encodeintoresponse(resp, factors); } }
with nothing else provided, tried things working first writing simple servlet in eclipse using dynamic-web module worked , modified servlet said in above example following code. time getting 404 error, how can test new servlet
implements servlet
instead
extends httpservlet
code tried:
package com.serialize; import java.io.ioexception; import java.io.printwriter; import java.math.biginteger; import javax.servlet.servlet; import javax.servlet.servletconfig; import javax.servlet.servletexception; import javax.servlet.servletrequest; import javax.servlet.servletresponse; import javax.servlet.annotation.webservlet; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; /** * servlet implementation class test1 */ @webservlet("/test1") public class test1 implements servlet { private static final long serialversionuid = 1l; /** * @see httpservlet#httpservlet() */ public test1() { super(); // todo auto-generated constructor stub } /** * @see httpservlet#doget(httpservletrequest request, httpservletresponse response) */ protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { // todo auto-generated method stub printwriter pw = response.getwriter(); pw.print("test1"); // service(request, response); } /** * @see httpservlet#dopost(httpservletrequest request, httpservletresponse response) */ protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { // todo auto-generated method stub } @override public void destroy() { // todo auto-generated method stub } @override public servletconfig getservletconfig() { // todo auto-generated method stub return null; } @override public string getservletinfo() { // todo auto-generated method stub return null; } @override public void init(servletconfig arg0) throws servletexception { // todo auto-generated method stub } @override public void service(servletrequest req, servletresponse resp) throws servletexception, ioexception { // todo auto-generated method stub biginteger = extractfromrequest(req); biginteger[] factors = factor(i); printwriter pw = resp.getwriter(); pw.print(factors); // encodeintoresponse(resp, factors); } private biginteger extractfromrequest(servletrequest req) { // todo auto-generated method stub string num = req.getparameter("factor"); return biginteger.valueof(long.parselong(num)); } private biginteger[] factor(biginteger i) { // todo auto-generated method stub long number = i.longvalue(); biginteger[] ret = null ; for(long j=2,indx=0;j<number/2;j++) { while(number%j == 0) { ret[(int) indx++]=biginteger.valueof(j); number = number / j; } } return ret; } }
any highly appreciated.
put code inside of doget method, not inside service method.
don't implement servlet, extend httpservlet class.
your code should similar 1 this tutorial
Comments
Post a Comment