JAVAJSP中通过Get和Post方式传递页面参数(三种方式)。

【JAVA】JSP中通过Get和Post方式传递页面参数(三种方式)。

一、通过FORM(表单)+Get+request.getParameter方式实现页面传参。

1、Login.jsp (登录页)

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
 
    
    <title>登陆页</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
 
  </head>
  
  <body>
  
  <form id="Form1" name="FormName" action="index.jsp" method="get">
 	
 	用户名:<input type="text" name="m_username" value="张飞"></br> </br>
          密 码:<input type="Password" name="m_password" value="123456">  </br></br>
	 <input type="submit" value="登陆">
  </form>
    
  </body>
</html>

2、index.jsp(接收参数页)

<%@ page language="java" import="java.util.*" pageEncoding="Utf-8"  %>
 
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>主页面</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
 
  </head>
  
  <body>
    
 	  <p>接收登陆信息如下: </p>
 	   登录名:
     <% 
		String n_username=request.getParameter("m_username");  // 读取login.jsp中输入的用户名,注意对应:m_username 
	    out.println(n_username);  // 输出变量
     %> 
     <br> <br>
     
 	  密码:
     <% 
		String n_password=request.getParameter("m_password");  // 读取login.jsp中输入的密码,注意对应:m_password 
	    out.println(n_password);  // 输出变量
     %> 
     <br> <br>
     
    
  </body>
</html>

二、通过URL地址+Get+request.getParameter方式实现页面传参。

1、login_1.jsp(传递参数页)

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>LOGIN登陆页</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
 
  </head>
  
  <body>
 	  <p>接收登陆信息如下: </p>
     <input name="注册" type="button" id="btn1" title="登注册" value="开始传参到index_2.jsp页"  onclick="location.href='index_1.jsp?name=张飞&sex=男'" />
  </body>
</html>

2、index_1.jsp(接收参数页)

<%@ page language="java" import="java.util.*" pageEncoding="Utf-8"  %>
 
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>JAVA - 主页面</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
 
  </head>
  
  <body>
    
 	  <p>接收登陆信息如下: </p>
     
 	  	   登录名:
     <%  
		String n_username=request.getParameter("name");  // 读取login_1.jsp中输入的用户名name 
	    out.println(n_username);  // 输出变量
     %> 
     <br> <br>
     
 	  密码:
     <% 
		String n_sex=request.getParameter("sex");  // 读取login_1.jsp中输入的密码,注意对应:sex 
	    out.println(n_sex);  // 输出变量
     %>      
     
    
  </body>
</html>

三、通过request.setAttribute+request.GetAttribute跳转页面传参

说明:除了在HTML中通过<jsp:forward 跳转,还可以通过<% response.sendRedirect(index_1.jsp?name=张飞) %>在JAVA代码中跳转。
1、login_1.jsp(传递参数页)

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>LOGIN登陆页</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
 
  </head>
  
  <body>
 
  <%
   // 通过setAttribute和getAttribute往【index_1.jsp】中传参。
      request.setAttribute("m_date","2020-10-01");
       request.setAttribute("m_time"," 12:00:01");
   %>
	
 	<jsp:forward page="index_1.jsp"/>  <%--跳转到index.jsp页面 --%>
    
  </body>
</html>

2、index_1.jsp(接收参数页,注意:会跳转页面)

<%@ page language="java" import="java.util.*" pageEncoding="Utf-8"  %>
 
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>JAVA - 主页面</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
 
  </head>
  
  <body>
    
 	  <p>接收登陆信息如下: </p>
     
 	 
     登陆时间:<% out.print(request.getAttribute("m_date"));out.print(request.getAttribute("m_time"));%>     
     
    
  </body>
</html>