Servlet和JSP.一个简单的要求? [英] Servlets and JSPs. A simple request?

查看:63
本文介绍了Servlet和JSP.一个简单的要求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了许多Java Server Page,并且我想通过添加Process Servlet(扩展HttpServlet)来使用Controller/View系统.

I have a number of Java Server Pages set up already and I would like to use a Controller/View system by adding a Process Servlet (which extends HttpServlet).

我只想在ProcessServlet添加一些属性后基本上按常规处理所请求的JSP.

I just want to basically process the requested JSPs as normal after the ProcessServlet has added some attributes.

说我所有的JSP都位于一个名为/content/的目录中,并且我的web.xml文件具有一个将/content/*.jsp映射到我的ProcessServlet的规则

Say all my JSPs are in a directory called /content/ and my web.xml file has a rule to map /content/*.jsp to my ProcessServlet

我一直无法找到将所有JSP移到不同目录(/content-JSPs/)的方法,这样就可以将它们分派到而不用无休止地遍历ProcessServlet了.

I have not been able to find a way short of moving all of my JSPs into a different directory (/content-JSPs/) so that they can be dispatched to without having to go through the ProcessServlet endlessly.

是否有一种方法可以基本上将#forward()(其他方法?)分派给所请求的JSP,而无需再次使其通过ProcessServlet?

Is there a way to basically dispatch#forward() (some other method?) to the requested JSP without having it go through the ProcessServlet again?

很难相信这种缺乏灵活性的存在.为什么Servlet不能仅充当对JSP的传递?

It's a bit hard to believe that this lack of flexibility exists. Why can't the Servlet just act as a pass through to the JSP?

我的目标是设置所有内容,以便Web服务器不必为所有JSP拥有单独的目录,而为其他所有内容(即CSS,JavaScript和图像)拥有另一个目录.我想保持目录结构(和URL结构)不变.

My goal here is to set everything up so that the web server does not have to have a separate directory for all JSPs and another directory for everything else i.e. CSS, JavaScript and images. I would like to keep the directory structure (and URL structure) as it is.

推荐答案

将它们放在/WEB-INF文件夹中.这也有效地使JSP隐藏在直接访问之外.您只需更改RequestDispatcher#forward()调用即可在路径中包含/WEB-INF.

Put them in /WEB-INF folder. This also effectively hides JSPs away from direct access. You only need to change the RequestDispatcher#forward() call to include /WEB-INF in the path.

request.getRequestDispatcher("/WEB-INF/content" + request.getPathInfo()).forward(request, response);

请注意,/content/*.jsp的URL模式在语法上无效.它应该是/content/*.也许您也确实使用过.要跳过静态资源,例如images/css/JS,您不应将其放在/content中,而应放在例如/resources/static等中.

Please note that the URL pattern of /content/*.jsp is syntactically invalid. It should be /content/*. Perhaps you have also really used that. To skip static resources like images/css/JS, you should just not put them in /content, but in for example /resources, /static, etc.

  • Design Patterns web based applications
  • Hidden features of JSP/Servlet

这篇关于Servlet和JSP.一个简单的要求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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