ServletRequest或HttpServletRequest对象具有用于映射参数名称及其值的映射对象。通过访问此映射,我们可以检查servlet请求中是否传递了参数。让我们看下面的例子。packageorg.nhooo.example.servlet;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importjava.io.IOException;
publicclassParameterCheckextendsHttpServlet{
protectedvoiddoGet(HttpServletRequestreq,HttpServletResponseres)
throwsServletException,IOException{
// 检查用户名参数是否存在
if(req.getParameterMap().containsKey("username")){
Stringusername=req.getParameter("username");
}
// 检查密码参数是否存在
if(req.getParameterMap().containsKey("password")){
Stringpassword=req.getParameter("password");
}
}
}