I developed a WebApplication using Netbeans and Glassfish server. I wanted to deploy that on the Tomcat server.But I was not able to run the servlet on Tomcat. I did the following
- Created a folder "fti" inside ROOT directory of Tomcat
- Placed index.jsp inside fti directory which I was able to access through my browser
- I created WEB-INF folder inside "fti" folder and put web.xml file inside it.
- I created classes folder inside "WEB-INF" folder and put the compiled java file test.class inside it.
- I tried calling the servlet through the browser by trying to access /fti/test and got the error as "The requested resource (/fti/test) is not available."
I was able to run the same servlet using the same web.xml file with Netbeans and glassfish server
My test.java
file:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class test extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet test</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet test </h1>");
out.println("</body>");
out.println("</html>");
}
}
@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";
}
}
My 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>test</servlet-name>
<servlet-class>test</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
try
withoutcatch
,finally
or resource declarations – Abhishek Nayak