Tomcat Working:-
Tomcat makes use of Sun Microsystems specific specifications.
Tomcat, developed by Apache (www.apache.org), is a standard reference implementation for Java servlets and JSP. It can be used standalone as a Web server or be plugged into a Web server
Tomcat, developed by Apache (www.apache.org), is a standard reference implementation for Java servlets and JSP. It can be used standalone as a Web server or be plugged into a Web server
1. Installation:-
You can download Tomcat fromhttp://tomcat.apache.org/download-60.cgi
Before running the servlet, you need to start the Tomcat servlet engine. To start Tomcat, you have to first set the JAVA_HOME environment variable to the JDK home directory.
By default, Tomcat runs on part 8080. You can change it to a different port.
2. Creating Servlet:-
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;
/**
*
* @author Bsb
*/
@WebServlet(urlPatterns = {"/firstServlet"})
public class firstServlet extends HttpServlet
{
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter())
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter())
{
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet firstServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet firstServlet at " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet firstServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet firstServlet at " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
@Override
public String getServletInfo()
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
@Override
public String getServletInfo()
{
return "Short description";
}// </editor-fold>
}
3. For Running Servlet on Apache Server:-
Open WEb Browser Type in this link:
return "Short description";
}// </editor-fold>
}
3. For Running Servlet on Apache Server:-
Open WEb Browser Type in this link:
http://localhost:8080/ProjectWeb/firstServlet
It will start the servlet on web server.
After deploying your application it will create war file.A WAR file known as Web application ARchive is similarly like a JAR file used to distribute a collection of JavaServer Pages, Java Servlets, Java classes, XMLfiles, tag libraries, web pages (HTML and related files) and other resources that together make a web application
you can check my link for more details:
https://wingsofm.blogspot.in/2017/04/difference-between-jar-war-and-ear.html
Similarly for jsp pages you can use tomcat server,you have to just open your web browser and tyoe this link:
http://localhost:8080/ProjectWeb/home.jsp
It will start the servlet on web server.
4. Deploying:
You can easilly deploy application you just created. In this case i created firstServlet application.After deploying your application it will create war file.A WAR file known as Web application ARchive is similarly like a JAR file used to distribute a collection of JavaServer Pages, Java Servlets, Java classes, XMLfiles, tag libraries, web pages (HTML and related files) and other resources that together make a web application
you can check my link for more details:
https://wingsofm.blogspot.in/2017/04/difference-between-jar-war-and-ear.html
Similarly for jsp pages you can use tomcat server,you have to just open your web browser and tyoe this link:
http://localhost:8080/ProjectWeb/home.jsp