登录的分离jsp

<%--
Created by IntelliJ IDEA.
User: admin
Date: 2022/6/5
Time: 14:56
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/login" method="post">
用户名:<input type="text" name="name"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="登录"/>
</form>

</body>
</html>
package com.song.sevrlet1;

import javax.servlet.ServletConfig;
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;

public class loginservlet extends HttpServlet {
public String name1;
public String password1;
@Override
public void init(ServletConfig config) throws ServletException {
name1 = config.getInitParameter("name");
password1 = config.getInitParameter("password");
}


@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String name = req.getParameter("name");
String password = req.getParameter("password");
if (name.equals(name1) && password.equals(password1)){
HttpSession session = req.getSession();
session.setAttribute("name",name);
req.getRequestDispatcher("session.jsp").forward(req,resp);
}else{
resp.sendRedirect("login.jsp");
}
}
}
<%--
Created by IntelliJ IDEA.
User: admin
Date: 2022/6/5
Time: 15:35
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
String s =(String) session.getAttribute("name");
%>
<%=
s
%>
</body>
</html>
posted @ 2022-06-05 16:15  小松2739  阅读(26)  评论(0)    收藏  举报