1.在 loginServlet extends HttpServlet:修改一下:
利用请求转发,传送消息
req.setAttribute(“msg”,“用户名或密码错误!”); //msg值
req.getRequestDispatcher("/login.jsp").forward(req,resp);//
req转发,网址不会改变,但内容会跳转页面:login.jsp
注意:/login.jsp,这个路径,要写:在WEB-INF中,(寻找)
直接写:/login.jsp,不用写:
/demo17_war/login.jsp
输入用户名,密码正确,
HttpSession session=req.getSession(); //利用session技术
session.setAttribute(“existUser”,existUser);//存储信息
一个session名称:existUser
package com.example.demo17;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
public class loginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//super.doGet(req, resp);//(这里不能写,报错405)
// req.setCharacterEncoding("utf-8");//这个加,中文乱码
//req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html");
resp.getWriter().println("Success!");
//String username= ServletContext.g
//String password=servletContext.getInitParameter("password");
String username=req.getParameter("username");
String password=req.getParameter("password");
System.out.println(username);
System.out.println(password);
User user=new User();
user.setUsername(username); //封装
user.setPassword(password);
String eee=user.getUsername();
System.out.println(eee);
UserModel userModel=new UserModel();
User existUser= null;
try {
existUser = userModel.login(user);
System.out.println(existUser);
} catch (SQLException throwables) {
throwables.printStackTrace();
System.out.println("错误!");
}
if(existUser==null){
// resp.sendRedirect("/demo17_war/login.jsp");//rssp重定向
req.setAttribute("msg","用户名或密码错误!");
//req.getRequestDispatcher("/demo17_war/index.jsp").forward(req,resp); 报错
//req.getRequestDispatcher("D:\\java web\\apache-tomcat-8.5.72\\webapps\\demo17_war\\login.jsp").forward(req,resp); //也报错
// resp.sendRedirect("/demo17_war/login.jsp");//rssp重定向
// req.getRequestDispatcher("/demo17_war").forward(req,resp); //这个是这个目录下寻找,所以:/login.jsp
//这个c'w
req.getRequestDispatcher("/login.jsp").forward(req,resp);
}
else {
HttpSession session=req.getSession();
session.setAttribute("existUser",existUser);
resp.sendRedirect("/demo17_war/success.jsp"); //resp的重定向
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// super.doPost(req, resp);
}
}
login.jsp修改:
java语言在<%%>中写:
值:<%=变量%>//
如果:输入·用户名,密码错误,就把requse数据,传到jsp里面,在
<%=%变量> //就能在网页看到信息
<%
request.setCharacterEncoding("utf-8");
String msg="";
if(request.getAttribute("msg") != null){ //请求数据如果不为空
msg=(String)request.getAttribute("msg"); //赋值
}
%>
<h3><%=msg%></h3>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<meta charset="GBK">
<html>
<head>
<title>登入页面</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String msg="";
if(request.getAttribute("msg") != null){
msg=(String)request.getAttribute("msg");
}
%>
<h3><%=msg%></h3>
<form action="/demo17_war/apple" method="get">
<table border="1" width="500">
<tr>
<td>用户名:</td>
<td><input type="textn" name="username"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="textn" name="password"></td>
</tr>
<tr>
<td>验证码:</td>
<td><input type="text" name="checkcode" size="6">
</td>
</tr>
<tr>
<td> <input type="checkbox" name="remember" value="true"> </td>
<td>记住用户名</td></tr>
<tr>
<td colspan="2"><input type="submit" value="登入"></td>
</tr>
</table>
</form>
</body>
</html>
success.jsp:成功跳转这个页面:
<% if(){ java代码 %>
放html代码(满足条件,显示这串代码,执行)
<% } else{
java代码
%>
html代码(else就会显示html代码,执行)
<% }%>//这里才结束了:else语句
<% if(request.getSession().getAttribute("existUser")==null
) { //判断语句,
%>
<%@ page import="com.example.demo17.User" %><%--
Created by IntelliJ IDEA.
User: yyy言者
Date: 2021/11/16
Time: 20:09
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=GBK" language="java" %>
<html>
<head>
<% if(request.getSession().getAttribute("existUser")==null
) {
%>
<h1>你还没有登录!请先去<a href="login.jsp">登录</a></h1>
<%}else{
%>
<h1>用户登录成功!</h1>
<%
User existUser=(User) request.getSession().getAttribute("existUser");
%>
<h3>您好<%=existUser.getUsername()%></h3>
<%
}%>
<title>Title</title>
</head>
<body>
</body>
</html>
11111111
<%User existUser=(User) request.getSession().getAttribute("existUser");%>
//创建User对象(在jsp)
//获取:session名称为existUser的信息,赋值给existUser
<h3>您好<%=existUser.getUsername()%></h3>//在页面打印出来
//获取User对象,你输入用户名的值
效果图:
直接访问:success.jsp,提示,还没有登入信息
出现:用户名或密码错误!
输入:正确的:用户名和密码:成功登入
防止乱码:
doget(){}:加这个防止中文乱码
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html"); //响应
在jsp和html中加这个:防止乱码:
charset=GBK
<%@ page contentType="text/html;charset=GBK" language="java" %>