0

a.jsp:

<form method=post action="b.jsp">
<INPUT TYPE="text" name="test" value="hello world!">
</form>

b.jsp:

<%@ page language="java" contentType="text/html;charset=UTF-8" %><%
response.sendRedirect("http://example.com/c.jsp");
%>

http://example.com/c.jsp: (this page in an other server)

<%@ page language="java" contentType="text/html;charset=UTF-8" %><%
System.out.println(request.getParameterValues("test")); //must use request.getParameter
%>

how can I get parameter test in c.jsp?

please don't using GET method like: response.sendRedirect("c.jsp?test=helloworld!");

I tried using forward,but throw exception:

404 Not Found
/acc/http:/example.com/ccc.jsp was not found on this server. 

thanks for help :)

AlanFoster
  • 8,156
  • 5
  • 35
  • 52
Koerr
  • 15,215
  • 28
  • 78
  • 108

3 Answers3

2

You can't (apart from the GET solution you proposed, and which in fact is not that bad).

Well, you can use the session to store the input value and remove it after you read it (something like a flash scope), but if mulitple tabs using the same session are used you may get in trouble with that.

That's if you want to use client-side redirect. If you can use forwarding (server-side redirect): request.getRequestDispatcher("/c.jsp").forward(request, response) - thus the request remains the same and you have the request parameters available.

And finally - don't do that in JSPs. Use servlets for writing your logic.

Update: since your c.jsp is on another server you don't really have any options (apart from GET)

It appears you can use a 307 redirect to make the redirect use post - check this question (it's about asp.net, but there are similar methods in the java response class)

Community
  • 1
  • 1
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • thanks @Bozho ,but I can't using forward,`c.jsp` in an other server, URL like :http://xxx.com/c.jsp – Koerr Apr 11 '11 at 09:03
  • thanks @Bozho,I see your Update,why can't analog
    post method in `b.jsp` ?
    – Koerr Apr 11 '11 at 09:12
  • 1
    @Zenofo - if you want the user to be directed to page `c` - no. sendRedirect is only GET. Check my update for a way to use post there. – Bozho Apr 11 '11 at 10:22
0

You can try request forward()(reference) or <jsp:forward>.(reference)

Harry Joy
  • 58,650
  • 30
  • 162
  • 207
0

Just let the form submit to that URL directly.

<form method=post action="http://example.com/c.jsp">
    <input type="text" name="test" value="hello world!">
</form>

No need for kludgy detours.

AlanFoster
  • 8,156
  • 5
  • 35
  • 52
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555