Getting error while servlet program HTTP Status 404 - /HelloWorldServlet/ - java

My Servlets program
package com.srccodes.example;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Helloworld
*/
#WebServlet("/Helloworld")
public class Helloworld extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public Helloworld() {
super();
// TODO Auto-generated constructor stub
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter printWriter = response.getWriter();
printWriter.println("<h1>Hello World!</h1>");
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
update :
wec.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">
<display-name>HelloWorldServlet</display-name>
<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>
while running above program on tomcat server am getting error
error --> http://i.stack.imgur.com/655Ew.png.
how to resolve this problem,please try to suggest me,good answers will appriciate

Their is nothing wrong in code you provided above.
your servlet name is 'Helloworld' and URL you are tryng is
http://localhost:8080/HelloworldServlet
if you want to run same servlet try
http://localhost:8080/HelloworldServlet/Helloworld
also Restart your server and check for URL.

Remove "Servlet" from the url, something like http://localhost:8080/Helloworld

Is this class part of a WAR application? Try putting the WAR name between the port and HelloWorld.

Related

requestDispatcher.forward() returns blank webpage

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);
}
}

On my servlet url-pattern, the application works with "/path" but not with "/path/to"

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>

Simple Servlet HTTP Status 405 - HTTP method GET is not supported by this URL [duplicate]

This question already has answers here:
HTTP Status 405 - HTTP method is not supported by this URL
(2 answers)
Closed 6 years ago.
I began studying servlets.
Code Servlet:
package arver;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* Created by 35717 on 30.03.2016.
*/
public class MainServlet extends HttpServlet {
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doGet(req, resp);
PrintWriter out = resp.getWriter();
out.print("servlet");
}
#Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
File 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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<servlet>
<servlet-name>MainServlet</servlet-name>
<servlet-class>arver.MainServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MainServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Server response:
HTTP Status 405 - HTTP method GET is not supported by this URL
type Status report
message HTTP method GET is not supported by this URL
description The specified HTTP method is not allowed for the requested resource.
Apache Tomcat/9.0.0.M4
why am I getting HTTP Status 405 - HTTP method GET is not supported by this URL error in this Program.
We extend the HttpServlet and #Override doPost but in our implementation we don't call its super since call to the super will give this message.
When you do super.doGet(request, response); in your Servlet's doGet() method, you actually call the doGet() of the HttpServlet class. So drop the super call. It's not needed.
Just remove these lines :
super.doGet(req, resp);
super.doPost(req, resp);
Use Either get or Post Method
MainServlet.java
package arver;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* Created by 35717 on 30.03.2016.
*/
public class MainServlet extends HttpServlet {
#Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
PrintWriter out = resp.getWriter();
out.print("servlet");
}
}
web.xml file
<?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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>MainServlet</servlet-name>
<servlet-class>arver.MainServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MainServlet</servlet-name>
<url-pattern>/MainServlet</url-pattern>
</servlet-mapping>
</web-app>
Url must be like this: http://localhost:8080/Project name/MainServlet

class not found exception for Servlet filter

I'm trying to create a basic servlet filter, I have created the filter and mapped it in the web.xml file but get filter not found exceptions.
Here's the web.xml file
<display-name>Disertation</display-name>
<filter>
<filter-name>AuthorizationFilter</filter-name>
<filter-class>Disertation.servlets.AuthorizationFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>AuthorizationFilter</filter-name>
<servlet-name>LoginServlet</servlet-name>
</filter-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>
Here is the path for the filter class: Disertation/src/servlets/AuthorizationFilter.java
Am i missing something or is my configuration wrong?
EDIT: I took the com out of the web.xml and Here is the filter class
package servlets;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import JavaBeans.User;
/**
* Servlet Filter implementation class AuthorizationFilter.
* Its purpose is to check logged-in user's role and
* and accordingly allow or prevent access to the web resources.
*/
public class AuthorizationFilter implements Filter {
private FilterConfig filterConfig;
/**
* #see Filter#init(FilterConfig)
*/
#Override
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig=filterConfig;
}
/**
* #see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
*/
#Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
ServletContext sc= filterConfig.getServletContext();
String username = request.getParameter("user");
String pwd = request.getParameter("pwd");
System.out.println("first check");
User user = new User (username, pwd, "employee");
System.out.println("is this getting here?");
request.setAttribute("role", user.getRole());
if (request.getAttribute("role").equals("employee")|| request.getAttribute("role").equals("admin"))
chain.doFilter(request, res);
}
/**
* #see Filter#destroy()
*/
#Override
public void destroy() {
filterConfig=null;
}
}
Your filter should be under the following directory Disertation/src/com/Disertation/servlets/AuthorizationFilter.java
to match your filter configuration or change your filter configuration to
<filter-class>servlets.AuthorizationFilter</filter- class>
Are you using the correct package? In AuthorizationFilter.java look for the package name that is declared at the top of the file. Then put that in your web.xml:
<filter-class>com.some.package.AuthorizationFilter</filter-class>
Also, there is a space in your closing tag after the hyphen

Http status 401 This request requires HTTP authentication (). in tomcat 6

I have developed a web application
if i run the application in tomcat 6 it asks for username and password
but after 3 attempsts it shows
the web.xml document is
<?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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>LoginRemote</display-name>
<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>
<servlet>
<description></description>
<display-name>remoteSample</display-name>
<servlet-name>remoteSample</servlet-name>
<servlet-class>com.src.remoteSample</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>remoteSample</servlet-name>
<url-pattern>/remoteSample</url-pattern>
</servlet-mapping>
<security-constraint>
<web-resource-collection>
<web-resource-name>Secure</web-resource-name>
<url-pattern>/*</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>*</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>Authentication required</realm-name>
</login-config>
<security-role>
<role-name>*</role-name>
</security-role>
</web-app>
i want to get the remote user's user name and password what should i do for it?
this is my servlet code
package com.src;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.tomcat.util.buf.Base64;
public class remoteSample extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public remoteSample() {
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
response.setContentType("text/html;charset=UTF-8");
System.out.println("login info:"+request.getRemoteUser());
System.out.println("Authentication type:"+request.getAuthType());
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
should i change something in my server's tomcat-server.xml and web.xml?
check that realm name you are using in server & remote user is matched with following name or not.
<realm-name>Authentication required</realm-name>
please check this link for more information regarding exception

Categories