CopyPastor

Detecting plagiarism made easy.

Score: 1; Reported for: Exact paragraph match Open both answers

Possible Plagiarism

Plagiarized on 2019-12-03
by Arvind Kumar Avinash

Original Post

Original - Posted on 2010-11-23
by BalusC



            
Present in both answers; Present only in the new answer; Present only in the old answer;

Typically, you write both the code in the same servlet in the sequence mentioned below:
A. Get the request parameters from the request (which may have values of HTML form elements and explicit request parameters)
String myObjectId = request.getParameter("myObjectId"); Object myObject = request.getSession().getAttribute(myObjectId); request.getSession().removeAttribute(myObjectId); // ...
B. Perform some business logic and forward the request to some JSP, as mentioned in the **Form preprocessing** part
String myObjectId = UUID.randomUUID().toString(); request.getSession().setAttribute(myObjectId, myObject); request.setAttribute("myObjectId", myObjectId); request.getRequestDispatcher("/WEB-INF/page.jsp").forward(request, response);
Learn how HTTP works:
- Client fires HTTP request. - Server retrieves HTTP request. - Servletcontainer creates new `HttpServletRequest` and `HttpServletResponse` objects. - Servletcontainer invokes appropriate servlet with those objects. - Servlet processes request and forwards request and response to JSP. - JSP writes to the response body. - Servletcontainer commits HTTP response. - Server sends HTTP response back to client and garbages request and response objects. - Client retrieves HTTP response and processes it (display HTML, apply CSS, execute JS).
When you send a new request by submitting the form, it won't reuse the same request and response objects.
There are two ways to overcome this stateless nature of HTTP. You need to convert this object to `String` and include it in a hidden input field of the HTML form in the JSP so that it'll be available as request parameter upon submission.
<input type="hidden" name="myObject" value="${myObjectAsString}" />
The conversion is necessary because HTTP and HTML doesn't understand Java objects. HTML is in Java's perspective basically one large `String` (do a rightclick and *View Source* in webbrowser to see it). If you don't convert a Java object to `String`, then by default Java object's `toString()` result will be printed to HTML.
Or, if the object is too large or too complex to be converted to `String` and vice versa, then you need to store it in the server's memory or in some database and instead pass its unique identifier around as hidden input value. Usually the session scope is been used for this.
Form preprocessing Servlet example:
String myObjectId = UUID.randomUUID().toString(); request.getSession().setAttribute(myObjectId, myObject); request.setAttribute("myObjectId", myObjectId); request.getRequestDispatcher("/WEB-INF/page.jsp").forward(request, response);
JSP example:
<input type="hidden" name="myObjectId" value="${myObjectId}" />
Form postprocessing Servlet example:
String myObjectId = request.getParameter("myObjectId"); Object myObject = request.getSession().getAttribute(myObjectId); request.getSession().removeAttribute(myObjectId); // ...


###See also:
- https://stackoverflow.com/questions/3106452/java-servlet-instantiation-and-session-variables - https://stackoverflow.com/questions/13292395/how-to-store-data-for-every-individual-browser-tab-window-the-session-scope-is/

        
Present in both answers; Present only in the new answer; Present only in the old answer;