学习Servlet的一个小练习
练习:
在web.xml文件中设置2个web应用的初始化参数,user 和 password,新建一个login.html,里面定义2个请求字段:user、password.发送到loginServlet。
在创建一个LoginServlet,在其中获取 请求的user和password,比对其和web.xml定义的参数是否一致,若一致,响应hello,用户名,若不一致响应:Sorry,用户名。
代码及步骤
1.在web.xml文件中设置初始化参数
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<!-- 配置当前web应用的初始化参数 -->
<context-param>
<param-name>user</param-name>
<param-value>root</param-value>
</context-param>
<context-param>
<param-name>password</param-name>
<param-value>1234</param-value>
</context-param>
</web-app>
2.新建login.html文件
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login Page</title>
</head>
<body>
<!-- 这里的action 的值 loginServlet 对应web.xml 中 Servlet配置中
<url-pattern>/loginServlet</url-pattern> 中“/”后面的值loginServlet,
其中“/”代表当前web应用的根目录-->
<form action="loginServlet" method="post" >
user:<input type="text" name="user"/><br>
password:<input type="password" name="password"/><br>
<input type="submit" value="Submit" />
</form>
</body>
</html>
3.创建LoginServlet
package com.first.test;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class LoginServlet implements Servlet{
@Override
public void destroy() {
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public String getServletInfo() {
return null;
}
//声明 ServletConfig 对象
private ServletConfig servletConfig;
@Override
public void init(ServletConfig servletConfig) throws ServletException {
this.servletConfig = servletConfig;
}
@Override
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
//1.获取 login.html 页面上提交的 user 和 password
String user = request.getParameter("user");
String pwd = request.getParameter("password");
//2.获取当前web应用的初始化参数 user 和password
//需要使用ServletContext对象
ServletContext servletContext = servletConfig.getServletContext();
String InitUser = servletContext.getInitParameter("user");
String InitPassword = servletContext.getInitParameter("password");
PrintWriter out = response.getWriter();
//3.比对
if(user.equals(InitUser) && pwd.equals(InitPassword)){
//4.打印信息
out.print("Hello," + user);
}else {
out.print("Sorry," + user);
}
}
}
4.配置LoginServlet,在web.xml文件中加入对Servlet的配置信息
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<!-- 配置当前web应用的初始化参数 -->
<context-param>
<param-name>user</param-name>
<param-value>root</param-value>
</context-param>
<context-param>
<param-name>password</param-name>
<param-value>1234</param-value>
</context-param>
<!-- 配置Servlet -->
<servlet>
<servlet-name>loginServlet</servlet-name>
<servlet-class>com.first.test.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>loginServlet</servlet-name>
<url-pattern>/loginServlet</url-pattern>
</servlet-mapping>
</web-app>
5.至此这个练习也算完成了,运行login.html,输入用户名和密码浏览器会返回对应的消息。