【JAVA】JSP中通过Get和Post方式传递页面参数。
程序员文章站
2022-05-08 13:30:40
...
一、通过FORM(表单)+Get方式实现页面传参。
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>