为什么我需要为Servlet而不是JSP映射或注释? [英] Why I need mapping or annotation for Servlet, but not for JSP?

查看:72
本文介绍了为什么我需要为Servlet而不是JSP映射或注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要向Servlet发出请求,我需要在XML文件中使用映射或为给定的Servlet添加注释.但是,为什么我不需要对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.

这是如何工作的?

index.html:

index.html:

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

result.jsp:

result.jsp:

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

注意,我不必使用任何XML映射或注释.它只是发现"了它.

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

但是这怎么行不通?

index.html:

index.html:

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

com.example.MyServlet:

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

在这里,我收到错误消息:请求的资源[/TestProject/com.example.MyServlet]不可用.如何?为什么我不需要使用XML,也不需要为JSP使用注释,但是却需要为Servlet使用注释?它们不应该以相同的方式工作,因为JSP最终转向Servlet.那么为什么会有不同的行为呢?我知道我缺少什么,我只是不知道...

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

推荐答案

为什么我需要为Servlet而不是为JSP映射或注释?

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

正如@SotiriosDelimanolis在上面的评论中所指出的,这并不是实际发生的情况. JSP最终变成了servlet,其行为类似于您自己定义的任何其他servlet.而且,当您定义servlet时,还需要添加URL映射,以便将URL路径解析为可以响应对该URL发出的请求的servlet.

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.

唯一的区别是,对于JSP文件,此映射由servlet容器隐式完成.对于您定义的servlet,您显然需要定义自己的映射,因为服务器不知道您想对自己的应用程序中的servlet做些什么.

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):

Web组件是servlet或JSP页面. web.xml部署描述符中的servlet元素用于描述两种类型的Web组件. JSP页面组件是通过使用隐式.jsp扩展映射在部署描述符中隐式定义的,或者是通过使用jsp-group元素显式定义的.

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.

[...] JSP页面转换为实现类以及部署信息.部署信息指示所需的支持类以及 JSP页面的原始URL路径与该页面的JSP页面实现的URL之间的映射.

因此,基本上,您的JSP被转换为servlet,并且从原始JSP页面位置路径到由此生成的servlet创建了映射.

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.

实际上没有执行JSP.执行的是从JSP生成的servlet.您必须意识到为方便起见提供了JSP.如果要从servlet生成HTML,则必须对整个HTML页面执行一整堆out.write("<html>"); out.write("\r\n");,依此类推.这不仅容易出错,而且这样做会发疯.因此,可以选择提供JSP并结合幕后完成的工作来使其全部正常工作.

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.

这篇关于为什么我需要为Servlet而不是JSP映射或注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆