为了账号安全,请及时绑定邮箱和手机立即绑定

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

@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 回答

text/html;charset=utf-8

0 回复 有任何疑惑可以回复我~

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

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



0 回复 有任何疑惑可以回复我~
<?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>


0 回复 有任何疑惑可以回复我~

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

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

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

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信