1. 怎么在多个JSP页面之间进行参数传递?需要使用JSP的内置作用域对象session。利用它的两个方法setAttribute(),getAttribute()
2. 下面的这个实例实现了把第一个JSP页面的参数传递给第三个页面的功能
3. 代码如下:1.jsp
<html>
<form method=get action=2.jsp>
what’s your name<input type=text name=username>
<input type=submit value=submit>
</form>
</html>
4. 2.jsp
<html>
<form method=post action=”3.jsp?pass=11″>
<%
String name=request.getParameter(”username”);
session.setAttribute(”username”,name);
%>
Your name is:<%=request.getParameter(”username”)%>
<br>what’s your hobby<input type=text name=hobby>
<input type=submit value=submit>
</form>
</html>
5. 3.jsp
</pre><p></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.2000007629395px;"><span style="color: rgb(0, 0, 255); font-size: 12px;"><span style="color: rgb(51, 51, 0);"><html> your name is:<%=session.getAttribute(”username”)%> <br> your hobby is:<%=request.getParameter(”hobby”)%> <br> your password is:<%=request.getParameter(”pass”)%> <br> </form></html></span></span></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.2000007629395px;"><span style="color: rgb(0, 0, 255); font-size: 12px;"><span style="color: rgb(51, 51, 0);"></span></span></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.2000007629395px;">以下是jsp跟servlet之间数据的传送</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.2000007629395px;"></p><pre name="code" class="html" style="color: rgb(51, 51, 0); line-height: 25.2000007629395px;"><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="shenfenChuli" method="post"> <input type="text" name="id"/> <input type="submit" value="提交"/> </form> </body> </html>
package com.LiDi.xhj; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class ShenfenChuli */ @WebServlet("/shenfenChuli") public class ShenfenChuli extends HttpServlet { private static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String id=request.getParameter("id"); int len=0; if (id.length()==15) { len=15; }else if(id.length()==18){ len=18; }else{ len=-1; } request.setAttribute("b", len); request.setAttribute("a", id); String path="One.jsp"; RequestDispatcher rd=request.getRequestDispatcher("/"+path); rd.forward(request, response); } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <%-- <jsp:useBean id="sfz" class="com.LiDi.xhj.ShenfenChuli" scope="session"></jsp:useBean> <jsp:param > --%> <% int len; String str=request.getAttribute("a").toString(); len=Integer.parseInt(request.getAttribute("b").toString()); String str5; if(len==18){ String str1=str.substring(6, 14); String str2=str1.substring(0, 4); String str3=str1.substring(4, 6); String str4=str1.substring(6, 8); str5=str2+"/"+str3+"/"+str4; }else{ out.print("输入的身份证号非法"); return; } %> <table width="322" border="0"> <tr align="center" bgcolor="#66FF00"> <td width="161">身份证</td> <td width="151">出生日期</td> </tr> <tr> <td> <% out.print(str); %> </td> <td> <% out.print(str5); %> </td> </tr> </table> </body> </html>