Advanced Java Programming - Old Questions

3. Describe the process to deploy the servlet. Write a program to a JSP web form to take input of a student and submit it to second JSP file which may simply print the values of form submission. [4+6]

10 marks | Asked in 2075

A servlet is normally deployed as part of a Web application, usually in the folder WEB-INF\\classes. Like any other java program we need to compile a servlet by using the java compiler javac and after compilation the servlet application , it would be deployed in a configuration environment and can be executed. This development environment setup involves following steps:
1. Download and install tomcat
2. Create a directory structure
3. Create a Servlet
4. Compile the Servlet
5. Create a deployment descriptor
6. Start the server and deploy the project
7. Access the servlet
Now, once you created servlet and the project is ready, There are several ways to deploy the project. They are as follows:
1. By copying the context(project) folder into the webapps directory
2 .By copying the war folder into the webapps directory
3. By selecting the folder path from the server
4. By selecting the war file from the server
Here, we are using the first approach. We need the servlet api to compile the servlet. We need to include JAR file in the CLASSPATH environment variable. Once the compilation succeeds, make a copy of web.xml file in "webapps\\hello\\WEB-INF" and make the following entries in the file.
<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>TestServlet</servlet-class>
</servlet>
</servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/TestServlet</url-pattern>
</servlet-mapping>
Restart the Tomcat server to refresh the "web.xml" file and invoke the servlet by issuing the URL: http://localhost:8080/myproject/TestServlet
Thus, above are the process to deploy the servlet.

Second Part:

//StudentForm.jsp

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

<!DOCTYPE html>

<html>

    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

        <title>Student Form.</title>

    </head>

    <body>

        <h1>Fill up the form!</h1>

        <form action="PrintDetail.jsp" method="POST">

            Name: <input type="text" name="name"><br><br>

            Roll No.: <input type="text" name="roll"><br><br>

            Address: <input type="text" name="address"><br><br>

            <input type="submit" value="Submit">   

        </form>

    </body>

</html>


// PrintDetail.jsp

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

<!DOCTYPE html>

<html>

    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

        <title>Print Student Details.</title>

    </head>

    <body>

        <h1>Student Details!</h1>

        Name: <%= request.getParameter("name") %><br>

        Roll No.: <%= request.getParameter("roll") %><br>

        Address: <%= request.getParameter("address") %><br>

    </body>

</html>