Monday, February 7, 2011

Invoking servlets from HTML forms

Question - "To invoke servlets from HTML forms".

To work on this program, I recommend you to download Netbeans IDE.

You can download Netbeans IDE from the following link,

http://netbeans.org/downloads/

After you download and install perform the following procedure,

Procedure to run this program on Netbeans
  • Start Netbeans IDE.
  • Now Select File->New Project.
  • In the New Project Dialog box that appears  select Java Web(Web Application).
  • Now “New Web Application” Dialog box appears.
  • Give the Project Name as “ServletApplication”.
  • Click Next twice and then Finish.
  • Now a web application named ServletApplication is created with a default page “index.jsp”. There write the code for HTML forms.
  • Then right click on the WEB-INF folder and select New-> Servlet. Give the name of the servlet as “MyServlet” , click Next. In the next dialog that appears please make sure “Add information to deployment descriptor” checkbox is selected. Now click next and then finish.
  • Write the code for Servlet in it.
  • To compile the file, right click on index.jsp and then select “compile file”.
  • After compiling, again right click on index.jsp and select “Run file”.
  • Now the browser(use firefox) will open and verify the output.
Download the Source code 


Code

index.jsp

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<html>
    <head><title>HTML Forms and Servlet</title></head>
    <body>
        <%-- <jsp:useBean id="beanInstanceName" scope="session" class="beanPackage.BeanClassName" /> --%>
        <%-- <jsp:getProperty name="beanInstanceName"  property="propertyName" /> --%>
        <h1>Enter your Details</h1>
        <form action = "MyServlet">
        <table>
        <tr>
        <td>Name
        <td><input type="text" name="name"><br><br>
        </tr>
        <tr>
        <td>Reg No
        <td><input type="text" name="reg"><br><br>
        </tr>
        <tr>
        <td>Sub1 Mark
        <td><input type="text" name="sub1"><br><br>
        </tr>
        <tr>
        <td>Sub2 Mark
        <td><input type="text" name="sub2"><br><br>
        </tr>
        <tr>
        <td>Sub3 Mark
        <td><input type="text" name="sub3"><br><br>
        </tr>
        <tr>
        <td>Sub4 Mark
        <td><input type="text" name="sub4"><br><br>
        </tr>
        <tr>
        <td>Sub5 Mark
        <td><input type="text" name="sub5"><br><br>
        </tr>
        <tr>
        <td>Sub6 Mark
        <td><input type="text" name="sub6"><br><br>
        </tr>
        </table>
        <input type="submit">
        </form>
    </body>
</html>


MyServlet.java

/*
 * MyServlet.java
 *
 * Created on February 3, 2011, 9:03 PM
 */
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
 *
 * @author  Administrator
 * @version
 */
public class MyServlet extends HttpServlet {
    /** Initializes the servlet.
     */
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }

    /** Destroys the servlet.
     */
    public void destroy() {

    }
      public String display(int mark)
    {
        if(mark>90 && mark<=100)
    {
    return "S";
    }
    else if(mark>80 && mark<=90)
    {
    return("A");
    }
    else if(mark>70 && mark<=80)
    {
    return("B");
    }
    else if(mark>60 && mark<=70)
    {
    return("C");
    }
    else if(mark>55 && mark<=60)
    {
    return("D");
    }
    else if(mark>=50 && mark<55)
    {
    return("E");
    }
    else
    {
    return("Re Appear");
    }
    }
    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        try {
            String name, reg;
            int mark,s1,s2,s3,s4,s5,s6;
name = request.getParameter("name");
reg = request.getParameter("reg");
s1 = Integer.parseInt(request.getParameter("sub1"));
s2 = Integer.parseInt(request.getParameter("sub2"));
s3 = Integer.parseInt(request.getParameter("sub3"));
s4 = Integer.parseInt(request.getParameter("sub4"));
s5 = Integer.parseInt(request.getParameter("sub5"));
s6 = Integer.parseInt(request.getParameter("sub6"));

response.setContentType("text/html");

out.println("<html>");
out.println("<head>");
out.println("<title>Mark Report Servlet<title>");
out.println("</head>");

out.println("<body>");
out.println("<center><p><h2><b>Name :" + name + "</p>");
out.println("<p><b>Reg No :" + reg + "</p>");
out.println("<p><b>Your Mark Details</p><br><br></center>");
out.println("<center><table border=2 cellpadding=5 cellspacing=5>");
out.println("<tr><th>Subjects</th><th>Mark</th><th>Grade</th></tr>");
out.println("<tr><th>Sub1</th><td>"+s1 + "</td><td>" + display(s1) + "</td></tr>");
out.println("<tr><th>Sub2</th><td>"+s2 + "</td><td>"  + display(s2) + "</td></tr>");
out.println("<tr><th>Sub3</th><td>"+s3 + "</td><td>"  + display(s3) + "</td></tr>");
out.println("<tr><th>Sub4</th><td>"+s4 + "</td><td>"  + display(s4) + "</td></tr>");
out.println("<tr><th>Sub5</th><td>"+s5 + "</td><td>"  + display(s5) + "</td></tr>");
out.println("<tr><th>Sub6</th><td>"+s6 + "</td><td>"  + display(s6) + "</td></tr></table>");
out.println("</body>");
out.println("</html>");
        } finally {
            out.close();
        }

    }

    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    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
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }

Note

Some parts of the code will be automatically generated by Netbeans itself. So you need not worry.

Screenshots




No comments: