ProcessRequest Method - java

When do ProcessRequest method called?
I am having a hard time why ,what and how the process request is called? why it is called and how it was called by the servlet container.

The servlet has two important methods for handling the client's request:
1. doPost: in general handles requests coming from forms with post method.
2. doGet: handled requests coming from get method.
Now, ProcessRequest method, is any other method that you can use into your code which is not bound (overridden) to anything.
It is called from the above methods to not complicate the code in them thus the requests are handled in it.
so you can use ProcessRequest to handle your request if and only if it's called from one of the methods above.

The only ProcessRequest I could find, and the example includes this
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
and
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
So, it's called when you call it.

Related

Java Servlet returns response to client before committing the database transaction

I use a 3rd party framework to process my requests by passing HttpServletRequest and HttpServletResponse into the framework. The database transaction handling is done separately from the framework like this:
public class MyServlet extends HttpServlet {
#Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
startTransaction();
framework.process(req, resp);
commitTransaction();
}
}
As it turns out, the framework writes the full response back to the client before the call to commitTransaction() returns. This creates possible race-conditions: The client might issue a follow-up request that runs in a second new database transaction that can't access the data added or updated in the first transaction, because it is not committed yet.
What are best practices to work around those kind of issues? I can't modify the behavior of the framework I'm using.
I suggest to use a ServletFilter for such concerns. It would look like this:
public void doFilter(
ServletRequest request,
ServletResponse response,
FilterChain chain) throws IOException, ServletException {
startTransaction();
chain.doFilter(request, wrapper);
commitTransaction();
}
However, you should add exception handling to the filter.

Servlet should accept POST and GET

I try to implement a servlet which should be called either through POST or GET.
So I wrote something like this
#Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// .. do stuff
// forward to welcome page
this.getServletContext().getRequestDispatcher("/guestbook.jsp").forward(req, resp);
return;
}
But/or because of the forward at the end I get an IllegalStateException, which is only a warning but still. What should I do differently?
Thanks,
-lony
Edit: Wanted Stacktrace
2012-05-26 18:02:16.422:WARN::/wsc/guestbook
java.lang.IllegalStateException: Committed
at org.eclipse.jetty.server.Response.resetBuffer(Response.java:1056)
at org.eclipse.jetty.server.Dispatcher.forward(Dispatcher.java:216)
at org.eclipse.jetty.server.Dispatcher.forward(Dispatcher.java:115)
at de.tum.in.dss.GuestbookController.doGet(GuestbookController.java:135)
at de.tum.in.dss.GuestbookController.doPost(GuestbookController.java:37)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:538)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1352)
at de.tum.in.dss.XSSFilter.doFilter(XSSFilter.java:76)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1323)
at de.tum.in.dss.AccessFilter.doFilter(AccessFilter.java:55)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1323)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:476)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:119)
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:517)
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:225)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:937)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:406)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:183)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:871)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:117)
at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:247)
at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:149)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:110)
at org.eclipse.jetty.server.Server.handle(Server.java:346)
at org.eclipse.jetty.server.HttpConnection.handleRequest(HttpConnection.java:589)
at org.eclipse.jetty.server.HttpConnection$RequestHandler.content(HttpConnection.java:1065)
at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:823)
at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:220)
at org.eclipse.jetty.server.HttpConnection.handle(HttpConnection.java:411)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:535)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:40)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:529)
at java.lang.Thread.run(Thread.java:722)
Your concrete problem is not related by letting GET and POST both do the same thing. Your problem is caused by writing to the response in the servlet and thus implicitly committing it before performing the forward.
Do not touch response.getWriter() or resposne.getOutputStream() in the servlet and just let JSP do that job. If you need to prepare data which JSP needs to display, just set it as an attribute in the request, session or application scope, depending on the scope the data needs to hold in.
See also:
Our servlets wiki page
Doing the same job on GET and POST is by the way smelly. Are you sure you understand what exactly each of those methods are to be used for?
You can override directly the service() method, it's called for all request methods.
replace this.getServletContext() with req

How to execute a method/event as soon as a servlet is called/page is loaded

I have a servlet that is called from a link on another page. The link actually references the servlet which then SHOULD write xml to the screen (outputting RSS XML information). Right now the link properly references and loads the servlet but because I have the code in the doPost method with nothing actually calling the doPost method nothing happens. (I'm new to Java EE) So how do I make that code execute without actually have a form that references the servlet through the "action =.." tag?
Can I call an init or main method that always executes on page refresh/load?
You can implement that logic in your doGet method. It has the same method signature as your doPost method.
Please see this thread
doGet and doPost in Servlets
For the difference between get vs post please see this article.
http://stevenclark.com.au/2008/01/12/get-vs-post-for-the-beginner/
You can also override Servlet.service method which is entry point for serving requests. This way you will handle both POST and GET requests.
Alternatively, you can implement logic in doGet method and invoke doGet from doPost:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
// do request processing
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doGet(request, response);
}

Servlet : doGet and doPost [duplicate]

This question already has answers here:
doGet and doPost in Servlets
(5 answers)
Closed 6 years ago.
I have 2 parameters, User and Pass. I want to send them to a servlet.
If I use the doGet method of the servlet it would look like this :
"link?User="+TextFieldValue+"&pass"textFieldValue
user= UserName.getValue();
pass= password.getValue();
Resource newPictureResource = new ExternalResource("http://localhost:8888/PieChart?UserName="+name+"&Password="+pass);
Success.setSource(newPictureResource);
editContent.addComponent(Success);
send them to servlet :
String UserName = request.getParameter("UserName");
String Password = request.getParameter("Password");
It works (tested)
If the Username + pass are right then he get a "success" picture posted on the screen.
But nobody passes the parameters via URL.
My question: How do I send the parameters using doPost method of the servlet ?
info : im working with eclipse on a liferay portal with a Vaadin portlet.
You don't send parameters in doPost(..). You receive them there. HTTP has many methods, two of which are GET and POST. It is up to the client-side to choose which method to use. POST is most often used with html forms - <form method="POST".
Vaadin should be able to send POST requests as well - see this thread
Not sure how Vaadin interacts, but typically portlet requests are handled differently. Looking though The Book of Vaadin - Portal Integration sheds some insights on configuration and action processing.
If you're looking for a way to handle both request types without reusing logic, simply choose your method of submission by either post or get from your application interface:
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Enter logic here
}

Is it mandatory to have a doGet or doPost method?

I have few questions.
Can i have a Servlet without these two methods?
Can i call my form directly to Service method... Like
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FormServlet extends HttpServlet {
protected void doService (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
No, it's not mandatory. Since HttpServlet is an abstract class, there are abstract implementations of all doXXX methods, and you don't have to implement them if you don't want to.
Yes, you can have a servlet without either of these methods (they have no implementation). Still having a HttpServlet without having doGet/doPost seems a bit pointless, since servlet can only communicate with the a limited number of request methods such as GET, POST, DELETE, PUT (for more see specification section 5.1.1 ).
HttpServlet don't have doService methods. If you meant void service() then I advise you not to mess with it unless you really know what you're doing.
If all you need is to use doService call it from doGet, doPost (as someone already suggested).
Ok, examples:
public class DoesNothingServlet extends HttpServlet {} //does what the name implies
public class FormServlet extends HttpServlet { //what you want to do
protected doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doService(request,response)
}
protected void doService (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//Do something
}
}
Read http://download.oracle.com/docs/cd/E17802_01/webservices/webservices/docs/1.6/api/javax/servlet/http/HttpServlet.html
Provides an abstract class to be subclassed to create an HTTP servlet suitable for a Web site. A subclass of HttpServlet must override at least one method, usually one of these:
doGet, if the servlet supports HTTP GET requests
doPost, for HTTP POST requests
doPut, for HTTP PUT requests
doDelete, for HTTP DELETE requests
init and destroy, to manage resources that are held for the life of the servlet
getServletInfo, which the servlet uses to provide information about itself
From your doGet and doPost methods, you can call your doService(..,..) method if you wish.
You will note that the service() method originates from the javax.servlet.GenericServlet and not javax.servlet.http.HttpServlet.
If you want to do something with a servlet which doesn't involve the http protocol, I would say go ahead.
In the Head First Servlets and JSP they explain this in detail. 99% You will make use of the HttpServlet.
By extending GenericServlet, the servlet would run regardless of the content submitted. As long as the URL is fired the service() method will execute.
My understanding, if I got you correct, is you want your HTTP GET, POST, (PUT, DELETE) to call your doService method. If that's the case, you can do this.
protected doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doService(request,response)
}
protected doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doService(request,response)
}
If you extends HttpServlet you don't have to override doGet and doPost as it's already implemented by HttpServlet. Servlet request get handled by the service() method which then (based on the HTTP request method) calls its relevant doXXX method.
I wouldn't mess with the service() method though, unless you know what you're doing.

Categories

Resources