为什么我这HTML中输出不了“用户名“和“密码”?

来源:2-2 练习题

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"));
   }
}

写回答 关注

4回答

  • 凌啊凌啊
    2018-09-07 11:57:26

    text/html;charset=utf-8

  • ATMYBEST
    2018-08-06 19:16:22

    既用了注解@WebServlet(name = "Servlet-init")

    又使用了web.xml配置,<servlet-name>ServletInit</servlet-name>



  • moummm
    2017-09-02 23:26:17
    <?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>


  • java_小牛
    2017-09-02 16:02:51

    web.xml 文件可以粘出来看看嘛

JAVA遇见HTML——Servlet篇

本门课程在JSP课程的基础上,深入介绍Servlet的基础知识

160615 学习 · 1029 问题

查看课程

相似问题