jsp+Servlet创建简单登陆页面
登陆页面的主要功能->
- 输入正确信息时跳转到welcome.jsp,并且浏览器保存正确信息
- 输入错误信息时显示“用户名或密码错误”
. - 再次登录时系统会显示保存的信息
.
4.未登录直接访问welcome.jsp时会提示请先登录
大体思路 ->
- 输入信息正确时,使用 response.sendRedirect重定向到welcome.jsp。
- 错误时,建立一个err,赋值“用户名或密码错误”,然后request.getRequestDispatcher请求转发到login.jsp。
- 注:request.getRequestDispatcher()是请求转发,前后页面共享一个request , 这个是在服务端运行的(不会跳转),对浏览器来说是透明的;response.sendRedirect()是重新定向,前后页面不是一个request。(跳转到其他页面)而这个是在浏览器端运行的
- 使用cookie语句来保存用户信息,并设置保存时间为1天。
- 向session中存放登录信息,当访问welcome.jsp时,如果uname存在,则欢迎,否则重新登录。
login.jsp:
<%@ 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>。。登录。。</title>
</head>
<body>
<h1>登录</h1>
<%
String err = (String) request.getAttribute("err");
out.print(err==null?"":err);
%>
<%
String uname=null;
String upwd=null;
Cookie cookies[]=request.getCookies();
for(Cookie co:cookies){
if(co.getName().equals("uname")){
uname=co.getValue();
}
if(co.getName().equals("upwd")){
upwd=co.getValue();
}
}
%>
<form action="/practice/LoginServlet" method="post">
用户名:<input type="text" name="uname" value=<%=(uname==null?"":uname) %>><br> <%--话说三元运算不太懂。value后的意思:uname是否为空,不是的话输出uname的值,是的话就输出空字符串 --%>
密 码:<input type="password" name="upwd" value=<%=(upwd==null?"":upwd) %> ><br>
<input type="submit" value="登录">
</form>
</body>
</html>
welcome.jsp:
<%@ 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>。。登录。。</title>
</head>
<body>
<h1>登录</h1>
<%
String err = (String) request.getAttribute("err");
out.print(err==null?"":err);
%>
<%
String uname=null;
String upwd=null;
Cookie cookies[]=request.getCookies();
for(Cookie co:cookies){
if(co.getName().equals("uname")){
uname=co.getValue();
}
if(co.getName().equals("upwd")){
upwd=co.getValue();
}
}
%>
<form action="/practice/LoginServlet" method="post">
用户名:<input type="text" name="uname" value=<%=(uname==null?"":uname) %>><br>
密 码:<input type="password" name="upwd" value=<%=(upwd==null?"":upwd) %> ><br>
<input type="submit" value="登录">
</form>
</body>
</html>
LoginServlet:
package Login;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String name = request.getParameter("uname");
String pwd=request.getParameter("upwd");
if(name.equals("Chy") && pwd.equals("111")){
HttpSession session = request.getSession();//向session中存放登录信息
Cookie cookie=new Cookie("uname",name);
Cookie cookiee=new Cookie("upwd",pwd);
response.addCookie(cookie);
response.addCookie(cookiee);
cookie.setMaxAge(60*60*24);
cookiee.setMaxAge(60*60*24);//设置保存时间为1天
session.setAttribute("uname", name);
session.setAttribute("upwd",pwd);
response.sendRedirect("/practice/welcome.jsp");//重定向到welcome.jsp
}else{
request.setAttribute("err", "用户名或密码错误");
request.getRequestDispatcher("/login.jsp").forward(request, response);//请求转发到login.jsp
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!