简体   繁体   中英

Why I need mapping or annotation for Servlet, but not for JSP?

To make a request to Servlet, I need to use mapping inside XML file or add annotation for given Servlet. But why I am not required to do the same for JSP files as well? I will give examples.

How does this work?

index.html:

<html><body>
 
    <form action="result.jsp">
        <button>go</button>
    </form>
    
</body></html>

result.jsp:

<html><body>
hello
</body></html>

Notice I didn't have to use any XML mappings nor annotations. It just "finds" it.

But how this doesn't work?

index.html:

<html><body>
 
    <form action="com.example.MyServlet">
        <button>go</button>
    </form>
    
</body></html>

com.example.MyServlet:

public class MyServlet extends HttpServlet {
 
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter pw = resp.getWriter();
        resp.setContentType("text/html");
 
        pw.println("hello");
 
    }
}

Here, I get error: The requested resource [/TestProject/com.example.MyServlet] is not available . How? How come I didn't need to use XML, nor annotations for JSP, but I had for Servlet? Shouldn't they work the same way, as JSP eventually turns to Servlet. So why is there different behavior? I know I am missing something, I just don't know what...

Why I need mapping or annotation for Servlet, but not for JSP?

As pointed out in the comment above from @SotiriosDelimanolis, that's not what actually happens. A JSP is eventually turned into a servlet and behaves like any other servlet you define on your own. And when you define a servlet you also need to add a URL mapping so that an URL path is resolved to a servlet that can respond to a request made for that URL.

The only difference is that for JSP files this mapping is done implicitly by the servlet container. For the servlets you define, you obviously need to define your own mapping because the server can't know what you want to do with the servlet in your own application.

The specifications says the following (emphasis mine):

A web component is either a servlet or a JSP page. The servlet element in a web.xml deployment descriptor is used to describe both types of web components. JSP page components are defined implicitly in the deployment descriptor through the use of an implicit.jsp extension mapping , or explicitly through the use of a jsp-group element.

and

[...] JSP page translated into an implementation class plus deployment information. The deployment information indicates support classes needed and the mapping between the original URL path to the JSP page and the URL for the JSP page implementation class for that page.

So basically, your JSP is translated into a servlet, and a mapping is created from your original JSP page location path to the servlet thus generated.

The JSP is not actually executed. What's executed is the servlet generated from the JSP. You have to realize that JSPs are provided for convenience. If you want to generate HTML from a servlet you have to do a whole bunch of out.write("<html>"); out.write("\r\n"); out.write("<html>"); out.write("\r\n"); and so on, for your entire HTML page. It's not only very error prone, but you will go insane doing so. Thus, the option of providing an JSP combined with the things done behind the scene to make it all work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM