Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>The standard way of passing/submitting data to the server in the pure Servlets/JSP world</strong> (as in your case from the JSP to the servlet) is by using <a href="http://www.w3.org/TR/html401/interact/forms.html" rel="nofollow noreferrer"><strong>HTML form</strong></a>, i.e. the same way as when using other technologies (ASP.NET, PHP etc). And it doesn't matter whether it is a pure HTML page or JSP page. The recommended/most used method of submitting data from the form to the server is <em>POST</em>. </p> <p>You also can <strong>pass data in the query string that is contained in the request URL</strong> after the path (this also happens when instead of <em>POST</em> you use <em>GET</em> method in the form). But that is for the simple cases, like constructing URLs for the pagination etc (you can see the example of constructing URLs with the additional queries here: <a href="https://stackoverflow.com/a/15712154/814702">Composing URL in JSP</a>)<br> Example of passing parameters in the URL:<br> <code>http://example.com/foo?param1=bar&amp;page=100</code></p> <p>For the <strong>difference between submitting data using <em>GET</em> and <em>POST</em> methods</strong> read here: </p> <ul> <li><p><a href="http://webdesign.about.com/od/forms/a/get-and-post-requests.htm" rel="nofollow noreferrer">GET versus POST Requests on HTML Forms</a></p></li> <li><p><a href="http://www.programmerinterview.com/index.php/general-miscellaneous/html-get-vs-post/" rel="nofollow noreferrer">In HTML forms, what’s the difference between using the GET method versus POST?</a></p></li> </ul> <p>So you can configure some <em>servlet</em> to process data sent/submitted from a JSP or HTML etc. It is highly recommended to submit data using <em>POST</em> method and respectively to process the submitted data using the <a href="http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServlet.html#doPost(javax.servlet.http.HttpServletRequest,%20javax.servlet.http.HttpServletResponse)" rel="nofollow noreferrer"><code>doPost()</code></a> method in your servlet. You will then get the parameters passed by the client in the request by using one of the following <a href="http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html" rel="nofollow noreferrer">ServletRequest</a> methods:</p> <ul> <li><a href="http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getParameter(java.lang.String)" rel="nofollow noreferrer">java.lang.String getParameter(java.lang.String name)</a></li> <li><a href="http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getParameterMap()" rel="nofollow noreferrer">java.util.Map getParameterMap()</a></li> <li><a href="http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getParameterNames()" rel="nofollow noreferrer">java.util.Enumeration getParameterNames()</a></li> <li><a href="http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getParameterValues(java.lang.String)" rel="nofollow noreferrer">java.lang.String[] getParameterValues(java.lang.String name)</a></li> </ul> <p>Here is a nice tutorial with examples: <a href="http://www.java-programming.info/tutorial/pdf/csajsp2/03-Form-Data.pdf" rel="nofollow noreferrer">Handling the Client Request: Form Data</a></p> <p>The above tutorial is from the following course:<br> <a href="http://courses.coreservlets.com/Course-Materials/csajsp2.html" rel="nofollow noreferrer">Building Web Apps in Java: Beginning &amp; Intermediate Servlet &amp; JSP Tutorials</a></p> <hr> <p><strong>Another way of exchanging data</strong> using Java EE is by storing data as attributes in different <strong>scopes</strong>. (Following is the excerpt from one of my answers on SO)</p> <p>There are <strong>4 scopes</strong> in Java EE 5 (see <a href="http://docs.oracle.com/javaee/5/tutorial/doc/bnafo.html#bnafp" rel="nofollow noreferrer">The Java EE 5 Tutorial: Using Scope Objects</a>). In Java EE 6 and in Java EE 7 there are <strong>5 scopes</strong> (see <a href="http://docs.oracle.com/javaee/6/tutorial/doc/gjbbk.html" rel="nofollow noreferrer">The Java EE 6 Tutorial: Using Scopes</a> and <a href="http://docs.oracle.com/javaee/7/tutorial/cdi-basic008.htm#GJBBK" rel="nofollow noreferrer">The Java EE 7 Tutorial: Using Scopes</a>). The most used are:</p> <ul> <li><em>Request scope</em></li> <li><em>Session scope</em></li> <li><em>Application scope</em> (Web Context)</li> </ul> <p>You can store some data in all the above scopes by setting the appropriate attribute.</p> <p>Here is a quote from the Java EE API docs related to <a href="http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#setAttribute(java.lang.String,%20java.lang.Object)" rel="nofollow noreferrer">ServletRequest.setAttribute(String, Object)</a> method in regard to <strong>request scope</strong>:</p> <blockquote> <pre><code>void setAttribute(java.lang.String name, java.lang.Object o) </code></pre> <p>Stores an attribute in this request. <strong>Attributes are reset between requests</strong>. This method is most often used in conjunction with RequestDispatcher.<br> ...</p> </blockquote> <p>So with every new request the previous attributes you have set in <em>request</em> will be lost. After you have set an attribute in a <em>request</em>, you <strong>must forward</strong> the request to the desired page. If you redirect, this will be a totally NEW request, thus the attributes previously set will be lost. (If you still want use redirection read this: <a href="https://stackoverflow.com/a/14638621/814702">Servlet Redirection to same page with error message</a>)</p> <p>Those attributes that are set in a <a href="http://docs.oracle.com/javaee/6/api/index.html?javax/servlet/http/HttpServletRequest.html" rel="nofollow noreferrer">HttpSession</a> (in the <strong>session scope</strong>) will live as long as the session lives and, of course, will be available to only the user to which session belongs.</p> <p>As for the context attributes they are meant to be available to the whole web application (<strong>application scope</strong>) and for ALL users, plus they live as long as the web application lives.</p> <p>Also maybe this article will be useful for you as well: <a href="http://www.devx.com/Java/Article/47975" rel="nofollow noreferrer">How Java EE 6 Scopes Affect User Interactions</a></p> <hr> <p>Also pay attention to the following <strong>issue</strong>. You wrote (quote):</p> <blockquote> <p>I know , We can pass variable from servlet to jsp by using request.setAttribute(key , value) But when I used it to set variable in jsp and again get it by using session.getAttribute(key ) then result is null. </p> </blockquote> <p>As the users @neel and @Sanchit have noticed, you are setting an attribute in the <code>request</code> object, but trying to get it back from the <code>session</code>. No wonder you are getting <code>null</code> in this case.</p> <hr> <p>Hope this will help you.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload