页面传递来账号密码,如果和后台写死的内容相同则相应登陆成功,不同则相应登陆失败
jsp页面代码,没学习过jsp的同学复制粘贴在项目的index.jsp里面即可
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<form method="post" action="login">
<p>
用户名:<input type="text" name="uname" >
</p>
<p>
密 码:<input type="password" name="pwd">
</p>
<p>
<input type="submit" value="提交">
</p>
</form>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>Userlogin</servlet-name>
<servlet-class>com.lin.servlet.UserLoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Userlogin</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
</web-app>
servlet实现类
package com.lin.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class UserLoginServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.设置请求编码格式
req.setCharacterEncoding("utf-8");
//2.设置响应编码格式
resp.setContentType("text/html;charset=utf-8");
//3.获取请求信息
String uname = req.getParameter("uname");
String pwd = req.getParameter("pwd");
//4.处理请求信息
if (uname.equals("张三") && pwd.equals("123")){
//5.响应处理结果
resp.getWriter().write("<h1> 登录成功 </h1>");
}else {
resp.getWriter().write("<h1> 账号或密码错误 </h1>");
}
}
}
网站声明:如果转载,请联系本站管理员。否则一切后果自行承担。
添加我为好友,拉您入交流群!
请使用微信扫一扫!