jsp+Servlet创建简单登陆页面
2023-06-13 09:18:28 时间
登陆页面的主要功能->
- 输入正确信息时跳转到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);
}
}
相关文章
- JSP热部署的实现原理[通俗易懂]
- 通过JSP端口转发拿下服务器权限
- Web阶段:第十二章:JSP动态页面
- Java+Servlet+JSP+Mysql+Tomcat实现Web学生选课管理系统
- "xxx cannot be cast to jakarta.servlet.Servlet "报错解决方式
- 2023-JavaWeb最新整理面试题-TCP、Tomcat、Servlet、JSP等
- 【项目实战】servlet+JDBC+jsp+mysql实现监考管理系统(增删改查)
- JSP跳转方式详解编程语言
- 利用JSP实现从MySQL中读取数据的简单操作(jsp显示mysql数据)
- servlet+jsp+java实现Web应用详解编程语言
- JSP标准标签库(JSTL)
- JSP jsp:include:页面包含标签
- JSP Response.removeAttribute()方法:移除指定的session属性
- 数据库快速搞定:JSP操作Oracle数据库(jsp操作oracle)
- JSP实现图片上传存入MySQL(jsp上传图片mysql)
- 如何使用JSP连接MySQL数据库(jsp如何连接mysql)
- Mysql与JSP连续不断的发展(mysql与jsp)
- JSP与MySQL的连接方式简介(jspmysql连接)
- 使用Linux轻松搭建JSP网站(linux搭建jsp)
- JSP与MySQL实现登录系统(jsp mysql 登陆)
- 使用JSP与Oracle实现数据库链接(jsp oracle链接)
- JSP与Oracle数据库实战指南(jsp oracle实战)
- jsp留言板源代码四:给jsp初学者.
- 我认为JSP有问题(下)
- Apache+Servlet+Jsp环境设置(上)
- JSP连接MySql/MSSQLServer/Oracle数据库连接方法[整理]
- jsp生成验证码代码
- jsp中页面之间的跳转forward与sendRedirect的区别
- JSP与Servlet的介绍说明
- 浅析javax.servlet.Servlet,ServletContext接口
- 利用jsp+Extjs实现动态显示文件上传进度