1

I want to read parameter passed by one JSP to another using HTTP POST method.

Following are my two JSP files.

One.jsp

<body>
<form action="Two.jsp" method="post">
    <input type="text" value="test value" name="txtOne">
    <input type="submit" value="Submit">
</form>
</body>

Two.jsp

<body>
    <% response.getWriter().println(request.getParameter("txtOne")); %>
</body>

I can access the parameter in Two.jsp file using scriplet.
I want to avoid scriplet, so I am looking for JavaScript or jQuery solution.
So far I have searched and found JavaScript solution which only reads parameters sent using GET method(query string only).

Any suggestion will be appreciated.

Thanks in advance.

Solution: I was able to get the value using JSTL:

${param.txtOne}
Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
Bhushan
  • 6,151
  • 13
  • 58
  • 91

4 Answers4

1

Try expression language - ${txtOne}, of if the method in one.jsp is GET instead of POST, you would be able to read URL in javascript and extract parameter value from there.

Ankit
  • 3,083
  • 7
  • 35
  • 59
1

Yes you can get the value by use of JSTL

${param.txtOne}

Update

From EL info page

In EL there are several implicit objects available.

EL                                  Scriptlet (out.print and null checks omitted!)
----------------------------------  ---------------------------------------------
${param.foo}                        request.getParameter("foo");
${paramValues.foo}                  request.getParameterValues("foo");
${header['user-agent']}             request.getHeader("user-agent");
${pageContext.request.contextPath}  request.getContextPath();
${cookie.somename}                  Too verbose (start with request.getCookies())

Implicit Objects

The JSP expression language defines a set of implicit objects:

  • pageContext: The context for the JSP page. Provides access to various objects including:

  • servletContext: The context for the JSP page’s servlet and any web components contained in the same application. See Accessing the Web Context.

  • session: The session object for the client. See Maintaining Client State.

  • request: The request triggering the execution of the JSP page. See Getting Information from Requests.

  • response: The response returned by the JSP page. See Constructing Responses.

In addition, several implicit objects are available that allow easy access to the following objects:

  • param: Maps a request parameter name to a single value

  • paramValues: Maps a request parameter name to an array of values

  • header: Maps a request header name to a single value

  • headerValues: Maps a request header name to an array of values

  • cookie: Maps a cookie name to a single cookie

  • initParam: Maps a context initialization parameter name to a single value

Finally, there are objects that allow access to the various scoped variables described in Using Scope Objects.

  • pageScope: Maps page-scoped variable names to their values

  • requestScope: Maps request-scoped variable names to their values

  • sessionScope: Maps session-scoped variable names to their values

  • applicationScope: Maps application-scoped variable names to their values

Community
  • 1
  • 1
Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
0

I want to avoid scriplet, so I am looking for JavaScript or jquery solution.

Simply you cannot.

request object can only accessible on server side, i.e in your JSP. You cannot access request or response object in client side that i.e javascript/jquery/whatever.

If you want access jsp value in javascript, try something like

 var news=<%= request.getParameter("txtOne")) %>;

As a side note: Avoid scriplets and go for Expression language.

Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0

It is time for you to move to some MVC framework like struts than plain JSP. Then you can fill an ActionForm from parameters and use them on Two.jsp.

Dheerendra Kulkarni
  • 2,728
  • 1
  • 16
  • 18