I have created a small dynamic web project to understand the mechanism of the Java Servlet but when I run the SimpleServlet
on server I am getting the HTTP Status 404
error but when typing this url http://localhost:8082/Test/
I am getting the content of the index.html file rendered. I dont have a web.xml file.
How can I run the servlet on the server?
SimpleServlet
package org.user;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
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;
@WebServlet(description = "A simple servlet", urlPatterns = { "/SimpleServletPath" })
public class SimpleServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
String userName = request.getParameter("userName");
HttpSession session = request.getSession();
// To use the username in different browser.
ServletContext context = request.getServletContext();
if (userName != null && !userName.isEmpty()) {
session.setAttribute("savedNameUser", userName);
context.setAttribute("savedNameUser", userName);
}
writer.println("Request parameter has username as " + userName
+ "</br>");
writer.println("Session parameter has username as "
+ (String) session.getAttribute("savedNameUser") + "</br>");
writer.println("Context parameter has username as "
+ (String) context.getAttribute("savedNameUser"));
}
}
image