相关疑难解决方法(0)

Servlet中的doGet和doPost

我已经开发了一个将信息发送到Servlet的HTML页面.在Servlet中,我使用的方法doGet()doPost():

public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException  {

     String id = req.getParameter("realname");
     String password = req.getParameter("mypassword");
}

public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {

    String id = req.getParameter("realname");
    String password = req.getParameter("mypassword");
}
Run Code Online (Sandbox Code Playgroud)

在调用Servlet的html页面代码中:

<form action="identification" method="post" enctype="multipart/form-data">
    User Name: <input type="text" name="realname">
    Password: <input type="password" name="mypassword">
    <input type="submit" value="Identification">
</form> 
Run Code Online (Sandbox Code Playgroud)

当我method = "get"在Servlet中使用时,我获得了id和password的值,但是在使用时method = "post",id和password被设置为null.为什么我不能在这种情况下获得值?

我想知道的另一件事是如何使用Servlet生成或验证的数据.例如,如果上面显示的Servlet对用户进行身份验证,我想在HTML页面中打印用户ID.我应该能够将字符串'id'作为响应发送,并在我的HTML页面中使用此信息.可能吗?

java servlets

105
推荐指数
1
解决办法
41万
查看次数

标签 统计

java ×1

servlets ×1