写给还是小白的我们,一起加油!
今天来一个巨简单的Servlet小练习,顺带温习下前两天的内容~
练习:
在web.xml文件中设置两个WEB应用的初始化参数,username,password,创建一个html页面,定义两个请求字段并发送到一个Servlet中,对应web.xml中的参数是否一样;若一致,响应hello:username;若失败则Sorry:username
html:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="PracticeServlet" method="post">
username:<input type="text" name="username"/>
password:<input type="password" name="password"/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
PracticeServlet:
package per.huang.pra;
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 PracticeServlet implements Servlet {
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public ServletConfig getServletConfig() {
// TODO Auto-generated method stub
return null;
}
@Override
public String getServletInfo() {
// TODO Auto-generated method stub
return null;
}
private ServletConfig servletConfig;
@Override
public void init(ServletConfig servletConfig) throws ServletException {
// TODO Auto-generated method stub
this.servletConfig=servletConfig;
}
@Override
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
//1,获取请求参数:username,password
String username=request.getParameter("username");
String password=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,比对
//4,打印响应字符串
if(initUser.equals(username)&&initPassword.equals(password)){
out.print("Hello:"+username);
}else{
out.print("Sorry"+username);;
}
}
}
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_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Day0516</display-name>
<!-- 配置当前WEB应用的初始化参数 -->
<context-param>
<param-name>user</param-name>
<param-value>huang</param-value>
</context-param>
<context-param>
<param-name>password</param-name>
<param-value>123456</param-value>
</context-param>
<!-- 配置Servlet -->
<servlet>
<servlet-name>PracticeServlet</servlet-name>
<servlet-class>per.huang.pra.PracticeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>PracticeServlet</servlet-name>
<url-pattern>/PracticeServlet</url-pattern><!-- 和跳转的action后面的内容一样 -->
</servlet-mapping>
</web-app>
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。