首先我们看一下servlet的一个例子:
<span style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 13.75px; line-height: 25.1953125px;">首先servlet继承于HttpServlet </span>
package com.test.action;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* servlet 继承与 HttpServlet 重写 doGet() doPost()方法
* servlet可以直接返回 ,即获取PrintWriter输出流,在PrintWriter中输出字符串
* servlet也可以返回jsp,即response.sendRedirect(jspPath)
*
* @author 姓名 工号
* @version [版本号, 2012-10-14]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
public class TestServlet extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
//直接返回
// resp.setContentType("text/html;charset=utf-8");
// PrintWriter pw = resp.getWriter();
// pw.println("<html><head>");
// pw.println("<title>testServlet</title>");
// pw.println("</head><body>this is a testServlet example!");
// pw.println("</body></html>");
// pw.close();
//返回jsp
resp.sendRedirect("servlet.jsp");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
// TODO Auto-generated method stub
doGet(req, resp);
}
}
web.xml的配置:
<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>com.test.action.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/testServlet</url-pattern>
</servlet-mapping>
首先从servlet-mappig中可以看出如果用户请求一个含有testServlet后缀的url时会调用名字为TestServlet的servlet,显然此servlet对应
com.test.action.TestServlet类,所以就会调用这个类中的doGet或doPost方法,那么到底调用那个方法呢,就取决于客户端的method是get还是post(比如form表单中会选择method)这两种方法可以互相调用,上面的例子演示了servlet即可直接返回html也可以返回jsp。
我们可以从<span style="font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; font-size: 11.875px; line-height: 17.998046875px; background-color: rgb(250, 250, 250);">HttpServletRequest 的实例req</span>中获取用户提交的参数,实例如下:(拿到用户输入的username)
<span style="font-family: Arial; font-size: 13.75px; line-height: 25.99609375px; background-color: silver;">String</span><span style="font-family: Arial; font-size: 13.75px; line-height: 25.99609375px;">username = req.getParameter(</span><span style="font-family: Arial; font-size: 13.75px; line-height: 25.99609375px; color: rgb(42, 0, 255);">"username"</span><span style="font-family: Arial; font-size: 13.75px; line-height: 25.99609375px;">); </span>
<span style="font-family: Arial;font-size:14px; line-height: 25.993303298950195px;">显然</span><span style="font-size:14px; line-height: 17.991071701049805px;"><span style="font-family:Monaco, DejaVu Sans Mono, Bitstream Vera Sans Mono, Consolas, Courier New, monospace;">我们同过resp返回客户端需要的信息。</span></span>
<span style="font-size:14px; line-height: 17.991071701049805px;"><span style="font-family:Monaco, DejaVu Sans Mono, Bitstream Vera Sans Mono, Consolas, Courier New, monospace;">
</span></span>
<span style="font-family:Monaco, DejaVu Sans Mono, Bitstream Vera Sans Mono, Consolas, Courier New, monospace;font-size:14px;"><span style="line-height: 17.991071701049805px;">所以总结一下流程:客户端请求特定url----->根据mappering配置找到处理这个请求的类(请求和处理之后的结果封装在<span style="font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; font-size: 11.875px; line-height: 17.998046875px; background-color: rgb(250, 250, 250);">req和<span style="font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; font-size: 11.875px; line-height: 17.998046875px; background-color: rgb(250, 250, 250);">resp中</span></span>)-------->返回给客户信息</span></span>