| | | Re: I created a servlet, from which I usually extend, by... The same happens when you use an IDE such as IBM Visual Age for Java. Author: Nirav Patel (http://www.jguru.com/guru/viewbio.jsp?EID=424727), May 18, 2001 Exactly... When you create a new Servlet, Visual Age for Java automatically creates the init(), doGet(..), doPost(..), and performTask(..) method. It also invokes the performTask(..) method from within the doGet(..) and doPost(..) methods. So, now when the user invokes either of the doGet() or doPost() method from the HTML Form using GET and POST, the query is automatically redirected to performTask(..) method. |
| | | Re: I created a servlet, from which I usually extend, by... Author: Ajay P (http://www.jguru.com/guru/viewbio.jsp?EID=1234517), Mar 24, 2005 Hi! When i am trying to compile the code using ApacheTomcat on IE i am getting the entire code as an output display. How am i suppose to run this servlet? Should i be saving the code as WebAppServlet.html? Help me out with this.If i am conceptually wrong then please tell me as what concept is involved with the execution. Thank you. |
| | | | Re[2]: I created a servlet, from which I usually extend, by... Author: Ajay P (http://www.jguru.com/guru/viewbio.jsp?EID=1234517), Mar 24, 2005 Hey i'm sorry.I need to store it in a java file. I don know as where i kept my brain that time. Its fine now. No need for any reply to the previous message. Thank you. |
| | | | | Re[3]: I created a servlet, from which I usually extend, by... Author: Mariusz Zaczek (http://www.jguru.com/guru/viewbio.jsp?EID=1268614), Oct 23, 2005 Nicola, thank you for including your code...that is very nice and clean and a good idea when you will be reusing the same general outline. |
Location: http://www.jguru.com/faq/view.jsp?EID=145
Created: Sep 3, 1999 Modified: 1999-12-22 11:10:54.113
Author: Alex Chaffee (http://www.jguru.com/guru/viewbio.jsp?EID=3)
For JWS, under Windows, pressing control-C doesn't fully shut down the server. You should use the Admin Tool and click "Shut Down". (Or you can hit ctl-alt-del, find "JREW" in the list, and "End Task".)
Comments and alternative answers
| | How do I fully shut down the server? Author: Rajan Kumar (http://www.jguru.com/guru/viewbio.jsp?EID=416254), May 6, 2001 You can shut down the JWS from admin tool; or alternativly by going to NT services and "stop" the "javawebserver" service. |
Location: http://www.jguru.com/faq/view.jsp?EID=146
Created: Sep 3, 1999 Modified: 1999-12-22 11:11:42.609
Author: Alex Chaffee (http://www.jguru.com/guru/viewbio.jsp?EID=3)
This is probably due to a NullPointerException being thrown. There's a bug in JWS 1.1 whereby it doesn't correctly log these exceptions.
The solution is to put your doPost() method inside a try block and catch NullPointerException. See the debugging question in this FAQ for more details and source code.
What is the HelloWorld Servlet?
Location: http://www.jguru.com/faq/view.jsp?EID=147
Created: Sep 3, 1999 Modified: 2001-12-29 11:08:03.612
Author: Alex Chaffee (http://www.jguru.com/guru/viewbio.jsp?EID=3)
import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class HelloHttpServlet extends HttpServlet{ public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { String name = req.getParameter("name"); if (name == null) name = "Joe"; res.setContentType("text/plain"); ServletOutputStream out = res.getOutputStream(); out.println("Hello, " + name + "!"); }}This code responds to an invocation of the form
http://myserver.foo.com/servlet/HelloHttpServlet?name=FredComments and alternative answers
| | Don't forget to include imports!!!! Author: Sharat N (http://www.jguru.com/guru/viewbio.jsp?EID=502507), Oct 16, 2001 (for new guys)In the above program, include http imports. import javax.servlet.*; import javax.servlet.http.*; import java.io.*; Thanks Sharat |
| | function setContentType not found in Tomcat 3.2.1 Author: John Meagher (http://www.jguru.com/guru/viewbio.jsp?EID=813752), Mar 26, 2002 Whenever I javax helloWorld.java, I get this error: function setContentType(java.lang.String) not found in class javax.servlet.http.HttpServletResponse Why would I get this message, if my classpath is ok? |
Location: http://www.jguru.com/faq/view.jsp?EID=148
Created: Sep 3, 1999
Author: Alex Chaffee (http://www.jguru.com/guru/viewbio.jsp?EID=3)
Use req.getRequestURI() or req.getServletPath(). The former returns the path to the script including any extra path information following the name of the servlet; the latter strips the extra path info. For example:
| URL | http://www.purpletech.com/servlets/HelloEcho/extra/info?height=100&width=200 |
| getRequestURI | /servlets/HelloEcho/extra/info |
| getServletPath | /servlets/HelloEcho |
| getPathInfo | /extra/info |
| getQueryString | height=100&width=200 |
out.println("<FORM METHOD=POST ACTION=\"" +res.encodeURL(req.getServletPath()) +
"\">");out.println("<INPUT NAME=value>");out.println("<INPUT TYPE=submit>");out.println("</FORM>");(encodeURL adds session information if necessary. See the Sun Servlet Tutorial and this FAQ's Session Tracking question. Note that this method was renamed from "encodeUrl" to the properly-capitalized "encodeURL" somewhere around version 2.1 of the servlet spec.)
Note that early versions of Java Web Server and some servlet engines had a bug whereby getRequestURI would also return the GET parameters following the extra path info. Comments and alternative answers
| | I tried to do this in IBM WAS 3.02 and got the error... Author: Avinash Kachhy (http://www.jguru.com/guru/viewbio.jsp?EID=224947), Oct 9, 2000 I tried to do this in IBM WAS 3.02 and got the error that the method req.getServletPath() was not defined. Is this correct for WAS or did I do something wrong? out.println("Servlet Path = " + req.getServletPath()); Thanks Avinash |
| | Use this piece of code to demonstrate the physical... Author: Aprameya Paduthonse (http://www.jguru.com/guru/viewbio.jsp?EID=4707), Feb 1, 2001 Use this piece of code to demonstrate the physical location of the servlet that gets invoked by a virtual path(URI) out.println(getServletConfig().getServletContext().getRealPath(request.getRequestURI())); |
Location: http://www.jguru.com/faq/view.jsp?EID=150
Created: Sep 3, 1999 Modified: 2003-01-08 14:30:31.956
Author: Alex Chaffee (http://www.jguru.com/guru/viewbio.jsp?EID=3)
This is actually a very complex issue. A few guidelines:
- The init() method is guaranteed to be called once per servlet instance, when the servlet is loaded. You don't have to worry about thread safety inside this method, since it is only called by a single thread, and the web server will wait until that thread exits before sending any more threads into your service() method.
- Every new client request generates (or allocates) a new thread; that thread calls the service() method of your servlet (which may in turn call doPost(), doGet() and so forth).
- Under most circumstances, there is only one instance of your servlet, no matter how many client requests are in process. That means that at any given moment, there may be many threads running inside the service() method of your solo instance, all sharing the same instance data and potentially stepping on each other's toes. This means that you should be careful to synchronize access to shared data (instance variables) using the synchronized keyword.
(Note that the server will also allocate a new instance if you register the servlet with a new name and, e.g., new init parameters.) - Note that you need not (and should not) synchronize on local data or parameters. And especially you shouldn't synchronize the service() method! (Or doPost(), doGet() et al.)
- A simple solution to synchronizing is to always synchronize on the servlet instance itself using &quot;synchronized (this) { ... }&quot;. However, this can lead to performance bottlenecks; you're usually better off synchronizing on the data objects themselves.
- If you absolutely can't deal with synchronizing, you can declare that your servlet &quot;implements SingleThreadModel&quot;. This empty interface tells the web server to only send one client request at a time into your servlet. From the JavaDoc: &quot;If the target servlet is flagged with this interface, the servlet programmer is guaranteed that no two threads will execute concurrently the service method of that servlet. This guarantee is ensured by maintaining a pool of servlet instances for each such servlet, and dispatching each service call to a free servlet. In essence, if the servlet implements this interface, the servlet will be thread safe.&quot; Note that this is not an ideal solution, since performance may suffer (depending on the size of the instance pool), plus it's more difficult to share data across instances than within a single instance.
See also What's a better approach for enabling thread-safe servlets and JSPs? SingleThreadModel Interface or Synchronization? - To share data across successive or concurrent requests, you can use either instance variables or class-static variables, or use Session Tracking.
- The destroy() method is not necessarily as clean as the init() method. The server calls destroy either after all service calls have been completed, or after a certain number of seconds have passed, whichever comes first. This means that other threads might be running service requests at the same time as your destroy() method is called! So be sure to synchronize, and/or wait for the other requests to quit. Sun's Servlet Tutorial has an example of how to do this with reference counting.
- destroy() can not throw an exception, so if something bad happens, call log() with a helpful message (like the exception). See the &quot;closing a JDBC connection&quot; example in Sun's Tutorial.
No comments:
Post a Comment