jsp登录页面
1.技术应用
1. GET,POST的区别
GET - 从指定的资源请求数据。
数据在 URL 中对所有人都是可见的。不安全
POST - 向指定的资源提交要被处理的数据
数据不会显示在 URL 中.安全
2. 请求转发与请求重定向
(1 ) RequestDispatcher.forward 方法只能将请求转发给同一个WEB应用中的组件;
而 HttpServletResponse.sendRedirect 方法不仅可以重定向到当前应用程序中的其他资源,
还可以重定向到同一个站点上的其他应用程序中的资源,
甚至是使用绝对URL重定向到其他站点的资源。
如果传递给HttpServletResponse.sendRedirect 方法的相对URL以“/”开头,
它是相对于整个WEB站点的根目录 ;如果创建RequestDispatcher对象时指定的相对URL以“/”开头,
它是相对于当前WEB应用程序的根目录 。
(2 )调用HttpServletResponse.sendRedirect 方法重定向的访问过程结束后,浏览器地址栏中显示的URL会发生改变,
由初始的URL地址变成重定向的目标URL;
而调用RequestDispatcher.forward 方法的请求转发过程结束后,
浏览器地址栏保持初始的URL地址不变。
(3 )HttpServletResponse.sendRedirect 方法对浏览器的请求直接作出响应,响应的结果就是告诉浏览器去重新发出对另外一个URL的访问请求
(4 )RequestDispatcher.forward 方法在服务器端内部将请求转发给另外一个资源,
浏览器只知道发出了请求并得到了响应结果,并不知道在服务器程序内部发生了转发行为
(5 )无论是 request.getRequestDispatcher (path).forward (request, response)还是response.sendRedirect ,
程序都会在执行完该句的情况下继续向下执行,因此在必要的时候应该使用return终止该方法
1. request.getServerName ()可以返回当前页面所在的服务器的名字;
2. request.getContextPath ()可以返回当前页面所在的应用的名字
3. request.getRealPath ("/" )返回物理路径
4. request.getRequestURI ()返回全称路径
5. request.getServletPath ()返回子路径
1.登录主页
<%@ 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 >
<%
//显示数据
String msg = (String )request .getAttribute("msg" );
if (msg != null ){
out.write(msg);
}
// <jsp:forward page="dologin.jsp" /> <!-- 它是直接在server 做的,浏览器并不知道 -->
%>
<form action ="/sh-web-jsp05/dologin.jsp" method ="get" >
账号:<input type ="text" name ="username" >
密码:<input type ="text" name ="password" >
账号:<input type ="submit" value ="登录" >
</from >
</body >
</html >
2.登录判断页
<%@ 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 >
<%
//获取参数
String username = request .getParameter("username" );
String password = request .getParameter("password" );
//处理逻辑
if (username.equals("wanglong" ) && password.equals("123" )){
//跳转页面-->欢迎页面 并且显示页面
//传值时 如果非表单提交的数据 可以保存在域对象中
//如果是非表单提交过来的数据 直接使用请求转发就行了
out.print("欢迎登录" );
session.setAttribute("username" , username);//向session中存入username以"username标识"
response .sendRedirect(request .getContextPath() + "/Success.jsp" );
} else {
//相对于服务器内 不添加工程名 例如请求转发
//相对于服务器外(相对网址的8080 后) 需要添加工程名 例如请求重定向
//失败后 在页面中显示错误信息
//使用请求转发相对于比较合适
request .setAttribute("msg" , "登录失败" );
request .getRequestDispatcher("/login.jsp" ).forward(request , response );
//response .sendRedirect(request .getContextPath() + "/login.jsp" );
}
%>
</body >
</html >
3.登录成功页
<%@ 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 >
<h1 > 欢迎来到本网站</h1 >
<%
%>
</body >
</html >