Friday, 31 July 2015

Run JSP Servlet in Apache Tomcat No Eclipse

Dear viewers,

This post will help you to run JSP and Servlet in Apache Tomcat without using any IDE (for e.g. Eclipse, NetBeans)

Prerequisite:
1. Apache Tomcat
  - I have apache-tomcat-7.0.63 on my Fedora machine.
2. Java
  - I have java-1.7.0-openjdk-1.7.0.3.x86_64 installed on my machine




Program flow:
1. Create a JSP page (index.jsp). On this accept firstname and lastname.
2. After clicking on button, form is submitted to servlet (NewServlet.java) which in turns checks user input with static values.
3. If input given is correct, user will be notified the same.
4. In case of wrong credentials, user will be taken back to index.jsp from NewServlet.java and message will be notified to the user.



Project hierarchy in apache-tomcat-7.0.63/webapps directory

/webapps
     /Project
              /WEB-INF
                     /classes
                           NewServlet.java
                           NewServlet.class
                     web.xml
              index.jsp


Steps:
1. Run apache tomcat
[pavan@Pavan ~]$ Softwares/apache-tomcat-7.0.63/bin/startup.sh
Using CATALINA_BASE:   /home/pavan/Softwares/apache-tomcat-7.0.63
Using CATALINA_HOME:   /home/pavan/Softwares/apache-tomcat-7.0.63
Using CATALINA_TMPDIR: /home/pavan/Softwares/apache-tomcat-7.0.63/temp
Using JRE_HOME:        /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.3.x86_64
Using CLASSPATH:       /home/pavan/Softwares/apache-tomcat-7.0.63/bin/bootstrap.jar:/home/pavan/Softwares/apache-tomcat-7.0.63/bin/tomcat-juli.jar
Tomcat started.



2. Create JSP file - index.jsp
<!-- index.jsp
Author: Pavan
-->

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>

<%@page import="javax.servlet.*"%>
<%@page import="javax.servlet.http.*"%>

<html>
    <head>
        <title>    Simple Web Application </title>
    </head>
    <body bgcolor="pink">
        <form action = "NewServlet" method="post">
            FirstName: <input type="text" name="fname" /> <br>
            LastName: <input type="text" name="lname" /> <br>
                  <input type="submit" value="ClickMe" />
        <%
            if(request.getAttribute("Error")!= null){
        %>
            <h3> <%= request.getAttribute("Error") %> </h3>
        <%
            }
        %>
        </form>
       
    </body>
</html>


3. Create servlet - NewServlet.java
/*
NewServlet.java
Author: Pavan
*/

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import javax.servlet.RequestDispatcher;

public class NewServlet extends HttpServlet{

    public void doPost(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException{
        PrintWriter out = response.getWriter();
        response.setContentType("text/html");
        String fname = request.getParameter("fname");
        String lname = request.getParameter("lname");
        if(fname.equals("Pavan") && lname.equals("Jaiswal")){
            out.print("<html>");
            out.print("<body bgcolor=pink>");
            out.print("<h1> Thanks for identifying yourself !!!</h1>");       
            out.print("</body>");
            out.print("</html>");
        }   
        else{
            request.setAttribute("Error","Incorrect credentials ");
            RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
            rd.forward(request,response);

        }

    }
}


4. Compile NewServlet.java
Note: While compiling servlet, classpath of "servlet-api.jar" has to be specified. Either you use "-cp" or "-classpath" option as shown below.

[pavan@Pavan apache-tomcat-7.0.63]$ javac -cp "lib/servlet-api.jar" webapps/Project/WEB-INF/classes/NewServlet.java

[pavan@Pavan apache-tomcat-7.0.63]$ ls webapps/Project/WEB-INF/classes/

NewServlet.class  NewServlet.java 


5. Prepare web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   version="2.5">
    <servlet>
      <servlet-name>NewServlet</servlet-name>
      <servlet-class>NewServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>NewServlet</servlet-name>
        <url-pattern>/NewServlet</url-pattern>
    </servlet-mapping>
</web-app>


6. Run project in browser
Give URL: http://localhost:8080/Project/index.jsp





 Give input other than "Pavan" "Jaiswal"





Congratulations !!! You have successfully run JSP and Servlet in Apache Tomcat without using any IDE.

Thanks.