0

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...

Stefan
  • 969
  • 6
  • 9
  • 2
    See the first bullet point of [this](https://stackoverflow.com/questions/10607415/how-does-jsp-work). The short version is that Servlet containers typically, internally, register a Servlet capable of compiling and serving JSPs. That servlet is registered to something like `*.jsp`. That's what handles your first example. – Sotirios Delimanolis Jan 10 '21 at 06:36

1 Answers1

1

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"); 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.

Bogdan
  • 23,890
  • 3
  • 69
  • 61
  • If JSP files get mapped internally by Container, how would it "see" if I mapped it in XML as well? Would it like overwrite it or what? We can't possibly have two mappings for single URL, can we? Because that could mean I can have two different configurations. Although this might sound easy for you, to me as a beginner it is rather confusing : – Stefan Jan 13 '21 at 09:46
  • 1
    **You can map `N URLs to 1 servlet`. You cannot map `N servlets to 1 URL`**. If you respects these, then you can have whatever configurations you want (implicit by the container or explicit by you). If there is a conflict, your server will complain and throw an exception on startup. – Bogdan Jan 13 '21 at 09:57
  • So in case I don't have explicit mapping for JSP file, Container would make one for us - using the same URL where JSP file is located. But if I had explicit mapping, it would use that one and not make any implicit mappings? Or that jsp-file (servlet) would have 2 URLs mapped to it - one explicit (from my own XML) and one implicit (from JSP's location in project)? Which "method" does it use from these 2 descriptions? – Stefan Jan 13 '21 at 11:47
  • 1
    You always get an implicit mapping for JSPs even if you add or not other mappings of your own. The "method" as you say, or better said, the mapping configuration that is used is the one that you specify when making a request. It's different doors that lead into the same room, so to speak. – Bogdan Jan 13 '21 at 15:51