1.创建一个login.jsp登陆界面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>login.jsp</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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<%-- action:我们需要提交的地址 method:请求的方式 --%>
<form action="doMain.jsp" method="get">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="userName"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td><input type="submit" value="登录"></td>
</tr>
</table>
</form>
</body>
</html>

2.创建对应的处理界面doMain.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"
contentType="text/html; charset=ISO-8859-1"
%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>doMain.jsp</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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<h1>登录成功</h1>
<%-- login.jsp中的form表单 get方式请求乱码
01.治标不治本 不推荐使用
String userName=new String(name.getBytes("iso-8859-1"),"utf-8");
02.治本
在服务器中的conf文件夹中找到server.xml文件中的Connector节点中 新增属性
URIEncoding="UTF-8"
--%>
<%
//根据login.jsp页面 name的属性值 获取 value
//post乱码 解决 是不是每个页面都需要设置 请求编码格式??? 后面 我们会用Filter
request.setCharacterEncoding("utf-8"); //请求的编码
response.setCharacterEncoding("utf-8"); //响应的编码
String name=request.getParameter("userName"); //获取用户名
String pwd=request.getParameter("password"); //获取密码 %> <%-- 就是想把login页面的值 传递给last.jsp --%>
<%
out.print("用户名:"+name);
out.print("密码:"+pwd);
//把从login页面拿到的值 存储到了 request作用域中了
request.setAttribute("userName", name);
request.setAttribute("password", pwd);
//转发到了last.jsp 携带了数据 last页面能取得数据
//request.getRequestDispatcher("last.jsp").forward(request, response);
//重定向last.jsp 数据都会丢失! last页面不能取得数据
response.sendRedirect("last.jsp");
%> <%-- get请求 --%>
<a href="last.jsp?userName=小黑黑2&password=123456">跳转到最后一个界面</a>
</body>
</html>

3.创建last.jsp看能不能获取login.jsp的值?

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>last.jsp</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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<h1>last.jsp</h1>
<%
//在doMain中使用了转发后 能直接获取 login页面的值
String name=request.getParameter("userName"); //获取用户名
String pwd=request.getParameter("password"); //获取密码
out.print("用户名:"+name);
out.print("密码:"+pwd);
%> <%
//从request.getAttribute()取值
request.setCharacterEncoding("utf-8"); //请求的编码
String name1=(String)request.getAttribute("userName");//获取用户名
String pwd2=(String)request.getAttribute("password"); //获取密码
out.print("用户名:"+name1);
out.print("密码:"+pwd2);
%>
</body>
</html>

4.通过request获取界面的多个值

01.创建request1.jsp

  <form action="request2.jsp" method="post">
<input type="checkbox" name="box" value="别玩手机">别玩手机"
<input type="checkbox" name="box" value="就玩手机">就玩手机"
<input type="checkbox" name="box" value="还玩手机">还玩手机"
<input type="checkbox" name="box" value="真玩手机">真玩手机"
<input type="checkbox" name="box" value="玩手机">玩手机">
<button type="submit">提交</button>
</form>

02.创建request2.jsp

 <body>
<%
request.setCharacterEncoding("utf-8");
//获取选中复选框的值
String [] boxs=request.getParameterValues("box");
//首先进行判断 必须先判断非空
if(boxs!=null&&boxs.length!=0){
for(String box:boxs){
out.print(box+"<br/>");
}
}else{
//重定向到request1界面
response.sendRedirect("request1.jsp");
}
%> <h1>request对象常用的方法</h1>
获取http请求中使用的方法名称 <%=request.getMethod() %><br/>
获取http请求中调用servlet的url部分 <%=request.getServletPath() %><br/>
获取http请求中MIME类型 <%=request.getContentType() %><br/>
获取请求中服务器主机名称 <%=request.getServerName() %><br/>
获取请求中服务器的端口号名称 <%=request.getServerPort() %><br/>
获取请求中服务器的ip地址 <%=request.getRemoteAddr()%><br/>
获取请求中服务器的ip地址 <%=request.getRemoteHost()%><br/>
获取请求中使用的协议 <%=request.getScheme() %><br/> </body>

 

转发和重定向

web02--jsp数据传递-LMLPHP

web02--jsp数据传递-LMLPHP

04-25 18:34
查看更多