实验四 Servlet应用开发
一、实验目的
1. 学会使用Servlet获取表单数据;
2. 学会使用Servlet的跳转方法实现重定向;
3. 学会配置和获取应用初始化参数的方法。
二、实验内容
1. 编写一个登录页面,根据登录验证结果,重定向到登录成功页面和登录失败页面;
2. 在Servlet中获取应用程序配置参数。
三、实验结果(源代码、运行截图)
LoginServlet.java
package xxx;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
ServletConfig config=this.getServletConfig();//拿到init方法中的ServletConfig对象
//获取当前Servlet在web.xml中配置的名称
String sName=config.getServletName();
System.out.println("当前Servlet在web.xml中配置的名称:"+sName);
// 获取当前Servlet在web.xml中配置的初始化参数
String right= config.getInitParameter("账号"); // 假设name是用于验证的用户名
String right2 = config.getInitParameter("密码"); // 假设password是用于验证的密码
//获取当前Servlet中配置的初始化参数(全部获取)
Enumeration enumeration=config.getInitParameterNames();
while(enumeration.hasMoreElements()) {
String name1 = (String) enumeration.nextElement();
String password1=config.getInitParameter(name1);
System.out.println(name1+":"+password1);
}
// 这里进行登录验证
if (right.equals(username) && right2.equals(password)) {
// 登录成功,将用户信息保存到session中
HttpSession session = request.getSession();
session.setAttribute("username", username);
// 重定向到登录成功页面
response.sendRedirect("success.jsp");
} else {
// 登录失败,重定向到登录失败页面
response.sendRedirect("fail.jsp");
}
}
}
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>LoginServlet</servlet-name>
<servlet-class>xxx.LoginServlet</servlet-class>
<init-param>
<param-name>账号</param-name>
<param-value>张嵩培</param-value>
</init-param>
<init-param>
<param-name>密码</param-name>
<param-value>123456</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
</web-app>
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>登录页面</title>
</head>
<body>
<form action="LoginServlet" method="post">
账号: <input type="text" name="username" required><br>
密码: <input type="password" name="password" required><br><br>
<input type="submit" value="登录">
</form>
</body>
</html>
fail.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录失败</title>
</head>
<body>
<h1>Login Fail!</h1>
</body>
</html>
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录成功</title>
</head>
<body>
<h1>Login Success!</h1>
<p>Welcome, <%= session.getAttribute("username") %></p>
</body>
</html>
标签:Web,Java,String,登录,servlet,import,Servlet,javax
From: https://blog.csdn.net/jianguosongzi/article/details/137355987