对servlet的 再总结 (精品)

 首先 可以通过URL 在浏览器端访问servlet,因为在web.xml中配置了URL与类全名的 映射. 

我们初学时,很容易分不清,浏览器端访问的 是jsp页面还是servlet.  其实当我们用浏览器访问servlet时,

servlet一般只有两种做法:

第一种,最简单,一般仅适用于试验测试. 就是用out对象输出 一个html页面.实质就是out.print("");方法可以在浏览器端 输出内容. (就像在后台输出内容一样) 

而一个很重要的点 就是这个方法中可以 输出 html标签. 将其输出为一个html页面. 并且默认是用doGet方法访问的.(原因不明??)

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;


public class HelloServlet extends HttpServlet {

    /**
     * Constructor of the object.
     */
    public HelloServlet() {
        super();
    }

    /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    /**
     * 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 {

        //response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        //out.println("<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">");
        //out.println("<HTML>");
        //out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
    //    out.println("  <BODY>");
        out.print("    This is ");
        out.println("doGet()  <br>");
        out.print(this.getClass());
        out.println(", using the GET method");
        out.println("  </BODY>");
        out.println("</HTML>");
        out.flush();
        out.close();
    }

    /**
     * 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();
        out.println("<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">");
        out.println("<HTML>");
        out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
        out.println("  <BODY>");
        out.print("    This is ");
        out.print("  怎么了不出现");
        out.print(this.getClass());
        out.println(", using the POST method");
        out.println("  </BODY>");
        out.println("</HTML>");
        out.flush();
        out.close();
    }

    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occurs
     */
    public void init() throws ServletException {
        // Put your code here
    }

}

当输出html标签时,这个页面就相当于是一个jsp页面,因为jsp页面就是这样 由<html>标签构成的.  这也让我们 分不清访问的究竟是servlet还是jsp页面.

而且访问servlet的URL是由web.xml文件的映射里面设置的,有一个默认的url(由/开始,这个/不能省),所以单独访问servlet类时,需要在浏览器中输出项目的url再加上这个URL.

第二种  这才是servlet的最重要的作用

在servlet中 实现向jsp页面的跳转,并传值.

但是,理论上是一个浏览器页面jsp页面 在上面点击一个操作,对应引发提交给一个URL连接.(那么这个URL就是一个servlet映射对应的URL,是不是绝对路径的URL??)

传给一个servlet类处理这个页面动作.(并且提交的时候选择时doGet方法访问还是doPost方法访问servlet,并且选择是不是表单传递参数)

所以这个servlet类,任务就是处理这个页面操作,调用相应的service 处理,并返回参数(这个是怎么做的,  好像处理后没有传参数?,但是数据库数据改变了,最后结果当你重新跳转jsp页面,这个页面非常重要,要显示最开始那个动作的实现的结果页面,应该与最开始的jsp页面只有一点小小的变化).所以就是调用一个重新查看数据库的jsp页面,也就是最初的页面.

那么有一个疑问就是:为什么最初的jsp页面会有对数据库的查询功能呢? 是因为我们最开始访问的jsp页面 其实就是访问的servlet  实现数据库扫描的作用 最后的跳转的jsp页面结果吗????

 是的,很聪明.因为单纯的jsp页面,是不可能读取到数据库信息的.所以我们一开始就要执行读取数据库数据的操作,就是我们第一次访问这个网站时,实质访问的就是一个servlet的URL. 在servlet里面处理读取数据库数据的操作,并将访问的数据存到内存,当做参数传递给 servlet最后跳转的那个jsp页面.

这里,又出现了一个重要问题,那就是 jsp中怎么接受 servlet中得到的结果 参数.

处理遍历时,老师并没有将参数存到session中,而是存到req.setAttribute  这个属性中

//向页面传值
            req.setAttribute("command", command);
            req.setAttribute("description", description);
            //这两个参数不传也可以,因为只有这用到,其它层当做参数传出去
            
            req.setAttribute("messageList", listService.query(command, description));
        

最后将req当做参数???传递jsp页面  (还要一个重要的事就是这个jsp是只能绝对路径吗,是一个什么样的路径,有多少种写法??)

req.getRequestDispatcher("/WEB-INF/jsp/back/list.jsp").forward(req,
                resp);

那么jsp页面怎么接受呢  这个参数,并传值给jsp页面并表示出来?

好像在jsp页面  使用jQuery方法还是OGNL方法直接调用java对象(即参数,参数就是java对象?或者类的属性)????

还有一个重要问题就是,为什么条件查询和 查询混在一起.??用一个动作 servlet ,这样简化了,性能高吗

原文地址:https://www.cnblogs.com/xuedexin/p/5665313.html