moummm
2017-09-02 01:00
@WebServlet(name = "Servlet-init")
public class Servletinit extends HttpServlet {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html,charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.println("<h1>"+"用户名:"+this.getUsername()+"</h1><br>");
out.println("<h1>"+"密码:"+this.getPassword()+"</h1><br>");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
public void init() throws ServletException{
this.setUsername(this.getInitParameter("username"));
this.setPassword(this.getInitParameter("password"));
}
}
text/html;charset=utf-8
既用了注解@WebServlet(name = "Servlet-init")
又使用了web.xml配置,<servlet-name>ServletInit</servlet-name>
<?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_3_1.xsd" version="3.1"> <servlet> <servlet-name>ServletInit</servlet-name> <servlet-class>com.servlet.Servletinit</servlet-class> <init-param> <param-name>username</param-name> <param-value>admin</param-value> </init-param> <init-param> <param-name>password</param-name> <param-value>admin</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>ServletInit</servlet-name> <url-pattern>/com/servlet/Servletinit</url-pattern> </servlet-mapping> </web-app>
web.xml 文件可以粘出来看看嘛
JAVA遇见HTML——Servlet篇
160615 学习 · 1029 问题
相似问题