Jsp servlet and pass each other between the values

一、JSP -> servlet

JSP page There are three ways to pass values ​​servlet: form form, URL, other

 

<!-- JSP page -->

...

<%......

       session.setAttribute("testSession","Hello session");

       request.setAttribute("testRequest","Hello request");

%>

<a href="JspServlet?action=toServlet">click me</a>

<form action="JspServlet?action=toServlet" method="post" name="form">

       <input name="username" type="test" />

       <input type="submit" value="submit">

</form>

...

 

1, the content of the JSP page form form, such as <input> tag, the servlet can request.getParameter ( "username"); Get.

2, URL: href attribute such as the tag here <a> <form> tag action attribute value "? JspServlet action = toServlet", in the same pick-servlet request.getParameter ( "action"); to be noted that the url corresponding to herein and in the path tag servlet web.xml Lane <url-pattern>. This will be mentioned later.

3, java code fragment, the servlet only to session.setAttribute ( "testSession", "Hello session") content, and then the contents of the request can not. (). GetAttribute ( "testSession") to obtain the content session with request.getSession in the servlet.

 

 

二、Servlet -> JSP

Go to jsp servlet from nothing more than two ways, redirect and url forwarding

 

1, redirection (Redirect): Skip is, the content and the changed path url. It does not allow the request parameter (session parameter), which does not allow to pass to the next page using the setAttribute method in the servlet request object. Use response.sendRedirect (url) method in the servlet. Note here without front url slash /, as response.sendRedirect ( "test.jsp")

 

2, url forwarding (Forward): is the page jump page content changes, url unchanged. You can take request and session parameters. Use getServletConfig in the servlet (). GetServletContext (). GetRequestDispatcher (url) .forward (request, response). While the former requires shaded here url / as getServletConfig (). GetServletContext (). GetRequestDispatcher ( "/ test.jsp"). Forward (request, response)

Guess you like

Origin www.cnblogs.com/zsboke/p/12130156.html