I have a servlet with url mapping ""(empty string). It's supposed to be mapped to the context root (I'm using tomcat7 with eclipse).
But sometimes when I refresh the browser, the page displays 404 page. And a quick fix is to re-run(inside eclipse). And after a while the same 404 error comes back.
I'm very frustrated about this. Is there a way to trace how on earth a mapped url pattern can sometimes (most of the time is OK) lead to 404?
EDIT 1:
OK, here is the details of configuration and code:
I have a apache httpd in front of the tomcat server.
The request is forwarded using mod_proxy
<IfModule mod_proxy_http.c>
ProxyPass /myapp http://127.0.0.1:8080/myapp
ProxyPassReverse /myapp http://127.0.0.1:8080/myapp
</IfModule>
And here is the servlet: (processRequest is called by both doPost and doGet)
#WebServlet("")
public class RootServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("Root Servlet");
try {
this.handleHomePage(request, response);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void handleHomePage(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, SQLException, NamingException {
HttpSession session = request.getSession(false);
if (session != null && session.getAttribute(Config.SESSION_AUTH_EMAIL) != null) {
String email = (String) session.getAttribute(Config.SESSION_AUTH_EMAIL);
request.getRequestDispatcher("/WEB-INF/jsp/main/home.jsp").forward(request, response);
}
else {
request.getRequestDispatcher("/WEB-INF/jsp/main/index.jsp").forward(request, response);
}
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.processRequest(request, response);
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.processRequest(request, response);
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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_3_0.xsd" id="WebApp_ID" version="3.0">
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/jsp/error/404.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/WEB-INF/jsp/error/500.jsp</location>
</error-page>
<servlet>
<servlet-name>info-about</servlet-name>
<jsp-file>/WEB-INF/jsp/info/about.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>info-about</servlet-name>
<url-pattern>/about</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>info-help</servlet-name>
<jsp-file>/WEB-INF/jsp/info/help.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>info-help</servlet-name>
<url-pattern>/help</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>info-contact</servlet-name>
<jsp-file>/WEB-INF/jsp/info/contact.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>info-contact</servlet-name>
<url-pattern>/contact</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>info-terms</servlet-name>
<jsp-file>/WEB-INF/jsp/info/terms.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>info-terms</servlet-name>
<url-pattern>/terms</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>info-privacy</servlet-name>
<jsp-file>/WEB-INF/jsp/info/privacy.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>info-privacy</servlet-name>
<url-pattern>/privacy</url-pattern>
</servlet-mapping>
</web-app>
The log printed by eclipse shows nothing. Just the 404.
I found the problem will occur when I edit something and save the changes. Eclipse will auto reload the context and home page returns 404.
OK, this might be a bug for tomcat 7.0.40
https://issues.apache.org/bugzilla/show_bug.cgi?id=54955
Related
Genuinely Stumped I'm new to Trying to Develop in java and I'm struggling with creating a user login page.
I want to check if the user has a session and has a key available that I've set at authentication if not I want them to be forwarded to the loginPage.jsp
The code that I've provided works when I don't use the following mapping for my servlet.
<servlet-mapping>
<servlet-name>ControllerServlet</servlet-name>
<url-pattern>/</url-pattern> -- This works by itself only works on paths not .jsp files
<url-pattern>*.jsp</url-pattern> -- if i include this i only get blank webpages
</servlet-mapping>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
<display-name>Project1</display-name>
<servlet>
<servlet-name>ControllerServlet</servlet-name>
<servlet-class>ControlServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ControllerServlet</servlet-name>
<url-pattern>/</url-pattern> //Works when by itself
<url-pattern>*.jsp</url-pattern> //breaks when i enabled this
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
Control Servlet.java
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class ControlServlet
*/
public class ControlServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public ControlServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// // TODO Auto-generated method stub
HttpSession session = request.getSession(false);
if (session == null) {
// Not created yet. Now do so yourself.
session = request.getSession();
} else {
// Already created.
}
//Check if User is Logged in
String pathInfo = (request.getPathInfo() != null) ? request.getPathInfo() : request.getServletPath();
System.err.println("check1");
System.err.println(session.getAttribute("name") == null);
System.err.println("check2");
System.err.println(!"/loginPage.jsp".equals(pathInfo));
System.err.println(pathInfo);
if (session.getAttribute("name") == null && !"/loginPage.jsp".equals(pathInfo))
{
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/loginPage.jsp");
requestDispatcher.forward(request, response);
//response.sendRedirect("loginPage.jsp");
System.err.println("did this ever happen?");
}else {
}
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
I am new to Java, seems like there is something wrong in my Servlet as I am keep getting net::ERR_TOO_MANY_REDIRECTS in the chrome browser and I am showing blank page for any URL I enter in the browser.
Below is the code:
web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
<display-name>My Application</display-name>
<session-config>
<session-timeout>30</session-timeout>
<cookie-config>
<name>mysessionid</name>
<http-only>true</http-only>
</cookie-config>
</session-config>
</web-app>
HomeServlet.xml
#WebServlet("/")
public final class HomeServlet extends HttpServlet {
private HttpSession session = null;
#Override
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
session = request.getSession(false);
System.out.println("session::: "+ session);
if(session != null && !session.isNew()) {
System.out.println("inside session only!");
response.sendRedirect("index.html");
} else {
something(request, response);
}
}
private String something(HttpServletRequest request, HttpServletResponse response) throws IOException {
if(accessToken != null) {
session = request.getSession();
response.sendRedirect("index.html");
}
}
Please tell me what went wrong above.
I'm trying to redirect my app to the login page in case the session has expired (I have set an interval of 60 minutes). I have created a filter but it seems the redirection is not working properly.
My web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<session-config>
<session-timeout>
60
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>login.html</welcome-file>
</welcome-file-list>
<filter>
<filter-name>RedirectFilter</filter-name>
<filter-class>LoginUtils.RedirectFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>RedirectFilter</filter-name>
<url-pattern>/AppUtils/*</url-pattern>
</filter-mapping>
</web-app>
My filter:
#WebFilter("/AppUtils/*")
public class RedirectFilter implements Filter {
private ServletContext context;
public void init(FilterConfig fConfig) throws ServletException {
this.context = fConfig.getServletContext();
this.context.log("RedirectFilter initialized");
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
String uri = req.getRequestURI();
this.context.log("Requested Resource::" + uri);
HttpSession session = req.getSession(false);
String loginURL = req.getContextPath() + "/login.html";
if (session == null && !req.getRequestURI().equals(loginURL)) {
this.context.log("Unauthorized access request");
res.sendRedirect(loginURL);
} else {
// pass the request along the filter chain
chain.doFilter(request, response);
}
}
public void destroy() {
//no-op
}
}
I have the following Java servlet:
package com.controller;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Teste extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
request.setAttribute("teste", "Test");
request.getSession().setAttribute("teste", "Test Session");
RequestDispatcher rd = request.getRequestDispatcher("teste.jsp");
rd.forward(request, response);
}
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
#Override
public String getServletInfo() {
return "Short description";
}
}
And the following web.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<servlet>
<servlet-name>Teste</servlet-name>
<servlet-class>com.controller.Teste</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Teste</servlet-name>
<url-pattern>/teste</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
I can access the servlet through the url http://localhost:8084/myapp/teste
What I want is change the url-pattern to /teste/edit, but when I do that, and try to access the servlet through the url http://localhost:8084/myapp/teste/edit I get the following 404 error:
HTTP Status 404 - /TrabalhoPSW/teste/teste.jsp
type Status report
message /TrabalhoPSW/teste/teste.jsp
description The requested resource is not available. Apache
Tomcat/8.0.27
Why is this happening? How can I fix this?
The problem was the JSP file that the servlet was trying to find. when I changed from
request.getRequestDispatcher("teste.jsp");
to
request.getRequestDispatcher("/teste.jsp");
It worked normally.
if you need than your servlet works with /path and /path/to, Change the mapping of servlet in web.xml to:
<url-pattern>/teste/*</url-pattern>
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>