public class CheckYearlyCardId extends HttpServlet { /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); YearlyCheckServiceDAO yearlyDao=new YearlyCheckServiceDAO(); String associatorCardId=request.getParameter("associatorCardId"); System.out.println(associatorCardId); if(yearlyDao.findYearlyCheckByassociatorCardId(associatorCardId)==true){ out.print("会员卡ID已存在!"); }else{ out.print("会员卡ID不存在!"); } }}给楼主一个例子 结合1楼理论 看看
String name=request.getParamete("name");
这就表示接收客户端请求name的参数response是服务器响应客户端请求,返回信息给客户端。。如:
response.sendRedirect("页面.jsp");
表示服务器处理了客户端的请求后,响应给客户端,客户端的浏览器就可以看到页面.jsp了。
public class CheckYearlyCardId extends HttpServlet { /**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { this.doPost(request, response);
} /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html");
PrintWriter out = response.getWriter();
YearlyCheckServiceDAO yearlyDao=new YearlyCheckServiceDAO();
String associatorCardId=request.getParameter("associatorCardId");
System.out.println(associatorCardId);
if(yearlyDao.findYearlyCheckByassociatorCardId(associatorCardId)==true){
out.print("会员卡ID已存在!");
}else{
out.print("会员卡ID不存在!");
}
}}给楼主一个例子 结合1楼理论 看看
自己去找servlet的源代码看看
我的意思是谁调用了这个doGet(request,response)方法
throws ServletException, IOException
{
String method = req.getMethod();
if(method.equals("GET"))
{
long lastModified = getLastModified(req);
if(lastModified == -1L)
{
doGet(req, resp);
} else
{
long ifModifiedSince = req.getDateHeader("If-Modified-Since");
if(ifModifiedSince < (lastModified / 1000L) * 1000L)
{
maybeSetLastModified(resp, lastModified);
doGet(req, resp);
} else
{
resp.setStatus(304);
}
}
} else
if(method.equals("HEAD"))
{
long lastModified = getLastModified(req);
maybeSetLastModified(resp, lastModified);
doHead(req, resp);
} else
if(method.equals("POST"))
doPost(req, resp);
else
if(method.equals("PUT"))
doPut(req, resp);
else
if(method.equals("DELETE"))
doDelete(req, resp);
else
if(method.equals("OPTIONS"))
doOptions(req, resp);
else
if(method.equals("TRACE"))
{
doTrace(req, resp);
} else
{
String errMsg = lStrings.getString("http.method_not_implemented");
Object errArgs[] = new Object[1];
errArgs[0] = method;
errMsg = MessageFormat.format(errMsg, errArgs);
resp.sendError(501, errMsg);
}
}