我已经开发了一个将信息发送到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页面中使用此信息.可能吗?
我有以下servlet:
public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 16252534;
private static int ping = 3000;
private Thread t;
private static boolean shouldStop = false;
@Override
public void init() throws ServletException {
super.init();
t = new Thread(new Runnable() {
@Override
public void run() {
while(!shouldStop) {
System.out.println("Now:" + System.currentTimeMillis());
try {
Thread.sleep(ping);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
t.start();
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
super.doGet(req, …Run Code Online (Sandbox Code Playgroud)