Java 如何在我的 Web 服务器 URL 中隐藏 .jsp 扩展名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/460288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to hide the .jsp extension in my web server urls
提问by
I have a JSP web server, with pages that all end with the .jsp extension.
我有一个 JSP Web 服务器,其页面都以 .jsp 扩展名结尾。
How can I hide it in my web server urls without resorting to non-java tricks (e.g., apache rewrite)?
如何在不诉诸非 Java 技巧(例如,apache 重写)的情况下将其隐藏在我的 Web 服务器 url 中?
For example: instead of typing http://www.sample.com/search.jsp?xxxthe user would just type http://www.sample.com/search?xxx
例如: 用户无需键入http://www.sample.com/search.jsp?xxx,而只需键入 http://www.sample.com/search?xxx
回答by Logan Serman
RewriteEngine On
RewriteRule ^([0-9a-zA-Z]+)$ .jsp
In your .htaccess should rewrite all URLs to .jsp. So search.jsp will just be search, as you described.
在您的 .htaccess 中应该将所有 URL 重写为 .jsp。所以 search.jsp 将只是搜索,正如您所描述的。
回答by Rahul
I am not a JSP expert but I think you can define URL-mappings in web.xml to provide aliases for your servlets and jsps.
我不是 JSP 专家,但我认为您可以在 web.xml 中定义 URL 映射来为您的 servlet 和 jsps 提供别名。
回答by kgiannakakis
You can create a servlet mapping like this:
您可以像这样创建一个 servlet 映射:
<servlet-mapping>
<servlet-name>MappingServlet</servlet-name>
<url-pattern>path/*</url-pattern>
</servlet-mapping>
The url-pattern must be edited to suit your needs. You need of course to create the servlet in order to map the url to the actual jsp. This technique is used by most of the MVC frameworks.
必须编辑 url-pattern 以满足您的需要。您当然需要创建 servlet 以便将 url 映射到实际的 jsp。大多数 MVC 框架都使用这种技术。
回答by mP.
Map the .jsp as a servlet, but use a <jsp-file> tag instead of a <url-mapping> tag.
将 .jsp 映射为 servlet,但使用 <jsp-file> 标记而不是 <url-mapping> 标记。
<servlet>
<servlet-name>myjsp</servlet-name>
<jsp-file>/myjsp.jsp</jsp-file>
</servlet>
回答by Peter Hilton
UrlRewriteis a good flexible Java-based framework-independent solution.
UrlRewrite是一个很好的灵活的基于 Java 的框架独立解决方案。
This is better than a Servlet mapping in web.xml
, because that is too limited in what you can do, and better than an Apache based solution because it is part of your web application so you do not need to put Apache in front of your application server.
这比 中的 Servlet 映射要好web.xml
,因为它的功能太有限,而且比基于 Apache 的解决方案要好,因为它是您的 Web 应用程序的一部分,因此您不需要将 Apache 放在应用程序服务器的前面。
回答by Ryan Watkins
If you opt for the Apache rewrite rule, rather than the application server mapping/filter (as I did) you might also want to do more than just look for "^([0-9a-zA-Z]+)$"
如果您选择 Apache 重写规则,而不是应用程序服务器映射/过滤器(就像我所做的那样),您可能还想做更多的事情,而不仅仅是寻找“^([0-9a-zA-Z]+)$”
You may want to confirm the url is not a directory or a file that does exist if apache is fronting and serving the non-jsp resources. And confirm that the JSP exists, and do a pass thru rather than redirect, and append any possible query string.
如果 apache 处于前端并为非 jsp 资源提供服务,您可能需要确认该 url 不是目录或确实存在的文件。并确认 JSP 存在,并执行传递而不是重定向,并附加任何可能的查询字符串。
RewriteCond %{REQUEST_URI} !^/.*\.(jsp)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME}.jsp -f
RewriteRule ^(.+)$ /.jsp [PT,QSA,L]
And to make sure that users only see this via /search, not /search.jsp then you want to rewrite the reverse as well
并且为了确保用户只能通过 /search 而不是 /search.jsp 看到这个,那么你也想重写相反的
RewriteRule ^(.+)\.jsp$ [R=301,QSA,L]
RewriteRule ^(.+)index$ [R=301,QSA,L]
This is a good idea for SEO purposes so that search engines dont ding you for duplicating content at multiple urls.
对于 SEO 而言,这是一个好主意,这样搜索引擎就不会因为在多个 url 上复制内容而责备您。
回答by Mehandi Hassan
Use this servlet mapping in your web.xml
file.
在您的web.xml
文件中使用此 servlet 映射。
<servlet>
<servlet-name>search</servlet-name>
<jsp-file>/search.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>search</servlet-name>
<url-pattern>/search</url-pattern>
</servlet-mapping>
回答by Paul Scott
I have a jsp located at data/feed.jsp Adding this to my web.xml meant I could access it at data/feed ...
我有一个位于 data/feed.jsp 的 jsp 将它添加到我的 web.xml 意味着我可以在 data/feed 中访问它...
<servlet>
<servlet-name>feed</servlet-name>
<jsp-file>/data/feed.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>feed</servlet-name>
<url-pattern>/data/feed</url-pattern>
</servlet-mapping>