0

guys, I want to pass values from one html page to another. In test1.html, submit the value to Serlvet. In servlet got the value, and dispatcher request to test2.html. like this:

request.setAttribute("url", url);
request.getRequestDispatcher("test2.html").forward(request,reponse);

So, how can i get the "url" value in test2.html?. need help, thx!

santi
  • 117
  • 3
  • 11

2 Answers2

2
request.setAttribute("url", url);
request.getRequestDispatcher("test2.jsp").forward(request,reponse);

then test2.jsp

<%@ page language="java" pageEncoding="UTF-8"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
${url}
</body>
</html>
KKL
  • 91
  • 8
  • is this mean can't use method above to pass values to html? (as the forward is server-side) @EJK – santi Nov 15 '13 at 03:53
  • Yes, you can't by using request.setAttribute("url", url); – KKL Nov 15 '13 at 04:00
  • Correct. HTML files are served by web servers. Web servers only provide static content. If you want dynamic content (i.e. this case), then you need something served by an application server. – EJK Nov 15 '13 at 04:10
  • okay, i see, guys. so what's the common method of pass variables between two html pages? By variables, i mean, like string, image, file etc. I tried cookies before, but this only for the string case. – santi Nov 15 '13 at 07:04
0

As the forward is entirely server-side, the attribute should still be present in the request. So from test2.html (actually you should make this a JSP page, test2.jsp), you can do the following:

<%
String url = (String)request.getAttribute("url");
%>

And if you wish to display it:

<html> ...
    The URL is: <%=url%>
</html>
EJK
  • 12,332
  • 3
  • 38
  • 55