I serve my app in localhost:8084/ues/. I want to redirect all request from localhost:8084/ues/* to localhost:8084/ues/index.jsp. How can I to do this?
In your web.xml have the below code,
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/ues/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.myPackage.redirectServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
public class redirectServletextends HttpServlet{
public void doPost(HttpServletRequest request, HttpServletResponse response)
{
try
{
RequestDispatcher dispatcher = request.getRequestDispatcher("/pages/index.jsp");
dispatcher.forward(request, response);
}
catch(Exception e)
{
//catch your exceptions
}
}
}
Define a filter in Deployment Descriptor like
<filter>
<filter-name>Filter-Name</filter-name>
<filter-class>Fully-Qualified-Class-Name</filter-class>
</filter>
<filter-mapping>
<filter-name>Filter-Name</filter-name>
<url-pattern>ues/*</url-pattern>
</filter-mapping>
and redirect to your desired Jsp from the Filter
Related
I'm following the demo in https://www.javatpoint.com/spring-mvc-tutorial. After running the demo on the tomcat server,I visited the url "http://localhost:8080/webTest1_war_exploded/",it seems the request is not handled by springmvc controller.
web.xml:
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Here is the controller code:
#Controller
public class HelloController {
public HelloController() {
}
#RequestMapping({"/"})
public String display() {
System.out.println("yes");
return "index";
}
}
after I visited the url: "http://localhost:8080/webTest1_war_exploded/", the console does not print "yes".
Can someone explain it to me?
Just try to replace
#RequestMapping({"/"})
with
#RequestMapping("/")
I have made a simple Servlet which is accessing a POJO object method in Netbeans.I have added Servlet details in web.xml also but on running the servlet class it is giving error
neither a main nor it is a servlet specified in web.xml
Here is my servlet class code..
public class Service extends HttpServlet {
protected void doGet (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
BLC blc = new BLC();
blc.captureCDRProcess();
} catch (Exception ex) {
System.out.println(ex);
}
}
}
And here is my Web.xml Code..
<servlet>
<servlet-name>Service</servlet-name>
<servlet-class>pojoserv.Service</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Service</servlet-name>
<url-pattern>/Service</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
In my application we have implemented several filters. For example I have three filters
Locale Filter
License Filter
Login Filter
The order is working fine in normal case. But when I am giving continuous refresh it sometimes skips the License Filter (execution then is: Locale Filter -> Login Filter).
Is there any way to avoid this? The order should not change even when I continuously refresh. The order should be followed every time.
public class LicenseFilter implements Filter {
#Override
public void destroy() {
}
#Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException {
try {
LOGGER.info("In LicenseFilter");
licenseValidator.validate();
chain.doFilter(req, res);
}catch (Exception excep) {
LOGGER.error("License Error " + excep.getMessage());
req.setAttribute("error", excep.getMessage());
RequestDispatcher dispatcher = req
.getRequestDispatcher("license_list.action");
dispatcher.forward(req, res);
}
}
}
#Override
public void init(FilterConfig cfg) throws ServletException {
WebApplicationContext wctx = WebApplicationContextUtils.getWebApplicationContext(cfg
.getServletContext());
licenseValidator = (licenseValidator) wctx.getBean("licenseValidator");
ctx = cfg.getServletContext().getContextPath();
}
}
web.xml
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>web</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>META-INF/beans-*.xml</param-value>
</context-param>
<filter>
<filter-name>localeFilter </filter-name>
<filter-class>com.filter.LocaleFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>localeFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>licenseFilter</filter-name>
<filter-class>com.filter.licenseFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>licenseFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>loginFilter</filter-name>
<filter-class>com.filter.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>loginFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
I have problems whit redirections and Servlets in Google App Engine.
I have an index.jsp and a list.jsp, but I can't get the results expected.
I have this in web.xml:
<filter-name>URIParserFilter</filter-name>
<filter-class>com.bbva.icaro2.web.filters.URIParserFilter</filter-class>
</filter>
<servlet>
<servlet-name>EntitiesAdminServlet</servlet-name>
<servlet-class>com.myproject.web.EntitiesAdminServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>ListServlet</servlet-name>
<servlet-class>com.myproject.web.ListServlet</servlet-class>
<jsp-files>/lists/lists.jsp</jsp-files>
</servlet>
<servlet-mapping>
<servlet-name>EntitiesAdminServlet</servlet-name>
<url-pattern>/admin/entities/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ListServlet</servlet-name>
<url-pattern>/lists/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
When I access to http://myproject/lists/mylist
The thread goes to URIParserFilter:
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
String entityKind = null;
String id = null;
String pathInfo = ((HttpServletRequest)req).getPathInfo();
String pathString = (pathInfo == null || pathInfo.equals("/")) ? "" : pathInfo.substring(1);
String[] parts = pathString.split("/");
entityKind = parts[0].trim();
id = (parts.length > 1) ? parts[1].trim() : null;
req.setAttribute(Constants.REQ_ATTR_REQ_ENTITYKIND, entityKind); // entityKind = "mylist"
req.setAttribute(Constants.REQ_ATTR_REQ_ENTITYID, id);
chain.doFilter(req, resp);
}
And then it goes to list.jsp whitout pass through ListServlet :-(
In case of http://myproject/admin/entities/hello it works!
The classes are exactly the same...
What I'm doing wrong?
Thanks, and sorry for my bad english...
write the only <jsp-files> with url pattern.it will redirect to jsp file.
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.uks.MyServlet</servlet-class>
<jsp-files>/jsp/my.jsp</jsp-files>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/MyServlet</url-pattern>
</servlet-mapping>
Do a forward from your servlet to your jsp page. Don't map the jsp in web.xml.
So do whatever you need to in your servlet and then:
String destination = "/jsp/my.jsp";
RequestDispatcher rd = getServletContext().getRequestDispatcher(destination);
rd.forward(request, response);
I have a simple Filter:
public class TestFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("before");
chain.doFilter(request, response);
System.out.println("after");
}
public void destroy() {
}
}
It's the first filter in web.xml and it has one of these two mappings:
<filter-mapping>
<filter-name>cookie-test-filter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
or
<filter-mapping>
<filter-name>cookie-test-filter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
In both cases I see the output:
before
before
after
after
(I've also tried INCLUDE as dispatcher just to be sure that everything works - there's no output with INCLUDE).
There're 3rd-party filters and servlets after this filter and I wonder: what should they do to make my filter applied in both described cases?
Try using a single <filter-mapping> entry with both REQUEST and FORWARD dispatcher
Example
<filter-mapping>
<filter-name>cookie-test-filter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
Hope this clears up the issue you were having.