Passing variables from JSP to servlet

2019-02-15 23:59发布

问题:

All the time, when I searched on Google, I got dozen of answers which are posted in Stackoverflow about passing variables to servlet from JSP. But I am wondering, I don't get answer of: How to pass a variable from JSP to a servlet class? Is it possible?

Actually I am doing a simple PhoneBook application. Here I have to send contact id to a servlet for editing and deleting. How can I pass this value?

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.

God help me.

回答1:

The standard way of passing/submitting data to the server in the pure Servlets/JSP world (as in your case from the JSP to the servlet) is by using HTML form, 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 POST.

You also can pass data in the query string that is contained in the request URL after the path (this also happens when instead of POST you use GET 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: Composing URL in JSP)
Example of passing parameters in the URL:
http://example.com/foo?param1=bar&page=100

For the difference between submitting data using GET and POST methods read here:

  • GET versus POST Requests on HTML Forms

  • In HTML forms, what’s the difference between using the GET method versus POST?

So you can configure some servlet to process data sent/submitted from a JSP or HTML etc. It is highly recommended to submit data using POST method and respectively to process the submitted data using the doPost() method in your servlet. You will then get the parameters passed by the client in the request by using one of the following ServletRequest methods:

  • java.lang.String getParameter(java.lang.String name)
  • java.util.Map getParameterMap()
  • java.util.Enumeration getParameterNames()
  • java.lang.String[] getParameterValues(java.lang.String name)

Here is a nice tutorial with examples: Handling the Client Request: Form Data

The above tutorial is from the following course:
Building Web Apps in Java: Beginning & Intermediate Servlet & JSP Tutorials


Another way of exchanging data using Java EE is by storing data as attributes in different scopes. (Following is the excerpt from one of my answers on SO)

There are 4 scopes in Java EE 5 (see The Java EE 5 Tutorial: Using Scope Objects). In Java EE 6 and in Java EE 7 there are 5 scopes (see The Java EE 6 Tutorial: Using Scopes and The Java EE 7 Tutorial: Using Scopes). The most used are:

  • Request scope
  • Session scope
  • Application scope (Web Context)

You can store some data in all the above scopes by setting the appropriate attribute.

Here is a quote from the Java EE API docs related to ServletRequest.setAttribute(String, Object) method in regard to request scope:

void setAttribute(java.lang.String name,
                  java.lang.Object o)

Stores an attribute in this request. Attributes are reset between requests. This method is most often used in conjunction with RequestDispatcher.
...

So with every new request the previous attributes you have set in request will be lost. After you have set an attribute in a request, you must forward 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: Servlet Redirection to same page with error message)

Those attributes that are set in a HttpSession (in the session scope) will live as long as the session lives and, of course, will be available to only the user to which session belongs.

As for the context attributes they are meant to be available to the whole web application (application scope) and for ALL users, plus they live as long as the web application lives.

Also maybe this article will be useful for you as well: How Java EE 6 Scopes Affect User Interactions


Also pay attention to the following issue. You wrote (quote):

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.

As the users @neel and @Sanchit have noticed, you are setting an attribute in the request object, but trying to get it back from the session. No wonder you are getting null in this case.


Hope this will help you.