我的Web应用程序中有一些jsp页面,以及web.xml
中的以下代码(针对每个jsp页面):
<servlet>
<servlet-name>login</servlet-name>
<jsp-file>/login.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
此设置会从我的网页网址中删除.jsp
,我可以/login
开启/login.jsp
实际上这个设置使我的jsp页面就像一个编译的servlet。
如果没有为每个页面编写此设置,是否仍然可以为我的所有jsp页面执行此操作?我需要某种url重写。
答案 0 :(得分:3)
您需要创建一个通用过滤器并处理请求并转发到相应的jsps 如果您使用的是旧版本的servlet,我可以在web.xml中编写带注释的编写器。 CommonFilter com.filters.CommonFilter
<filter-mapping>
<filter-name>CommonFilter</filter-name>
<url-pattern>*.*</url-pattern>
</filter-mapping>
此处所有请求都转发到相应的jsps。 例如..如果url是/ login,它将转发到/login.jsp,或者如果是/ home然后转发到/home.jsp ...所有url只被视为jsp文件,但你可以按照过滤网址你的要求
package com.filters;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
@WebFilter("*") // give your url mapping eg..//you can write for "/yoururlspec/*"
public class CommonFilter implements Filter {
@Override
public void init(FilterConfig config) throws ServletException {
}
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws ServletException, IOException {
HttpServletRequest request = (HttpServletRequest) req;
String path="";
if (request.getRequestURI().length() > request.getContextPath().length())
{path=request.getRequestURI().substring(request.getContextPath().length()+1, request.getRequestURI().length());
req.getRequestDispatcher(path+".jsp").forward(req, res);
}
}
@Override
public void destroy() {
}
}
答案 1 :(得分:1)
这是适用于我的代码:
package filters;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashSet;
@WebFilter("*")
public class URLFilter implements Filter {
HashSet<String> invalidExts = new HashSet<String>();
@Override
public void init(FilterConfig config) throws ServletException {
invalidExts.add(".css");
invalidExts.add(".js");
invalidExts.add(".img");
invalidExts.add(".png");
invalidExts.add(".jpg");
invalidExts.add(".jpeg");
invalidExts.add(".gif");
invalidExts.add(".woff");
invalidExts.add(".woff2");
invalidExts.add(".ttf");
invalidExts.add(".eot");
}
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws ServletException, IOException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
String context = request.getContextPath();
String uri = request.getRequestURI();
String qStr = request.getQueryString();
if (!uri.endsWith("/") && uri.length() > context.length() && isPage(uri)) {
if (uri.endsWith(".jsp")) {
String path = uri.substring(0, uri.lastIndexOf(".jsp"));
if (qStr != null && qStr.length() > 0)
path += "?" + qStr;
response.sendRedirect(path);
} else {
String path = uri.substring(context.length()).replace("-", "_");
req.getRequestDispatcher(path + ".jsp").forward(req, res);
}
} else chain.doFilter(req, res);
}
@Override
public void destroy() {
}
private boolean isPage(String uri) {
boolean result = true;
String ext = null;
if (uri != null) {
if (uri.contains("."))
ext = uri.substring(uri.lastIndexOf("."));
if (ext != null && invalidExts.contains(ext))
result = false;
}
return result;
}
}
答案 2 :(得分:0)
您可以尝试UrlRewriteFilter。
中的说明操作