Programs based on Servlet

a.Java application to demonstrate servlet life cycle.


 b.Develop a servlet web application to create a login form which take input -Name, Phone number, Address, Language known(i.e., Hindi, Marathi, Kannada etc.) and hobbies(Reading, Travelling, Singing etc). Show the Entered values. c.Write a servlet program to find prime number and factorial. Create error page for factorial if negative number is entered as input.


c.Write a servlet program to find prime number and factorial. Create error page for factorial if negative number is entered as input.

--------------------------------------------------------------

a.Java application to demonstrate servlet life cycle.

CODE:

Index.html

 

<!DOCTYPE html>

<!--

To change this license header, choose License Headers in Project Properties.

To change this template file, choose Tools | Templates

and open the template in the editor.

-->

<html>

    <head>

        <title>Servlet Practical</title>

        <meta charset="UTF-8">

        <meta name="viewport" content="width=device-width, initial-scale=1.0">

    </head>

    <body>

        <form name = "frm1" action = "servlet">

            <label>Enter Your Name:</label>

            <input type="text" name="txt1"/><br/>

            <input type="submit" value="Click Me!"/>

        </form>

    </body>

</html>

 

servlet.java

 

package servpack;

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

 

public class servlet extends HttpServlet {

    String str1;

   

    @Override

    public void init(ServletConfig config) throws ServletException

    {

        str1 = " Welcome to servlet!";

    }

 

    @Override

    protected void doGet(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        

        PrintWriter out  = response.getWriter();

        String str2 = request.getParameter("txt1");

        out.print("<h1>"+str2+str1+"<h1>");

        //out.println("<h1>"+str1+"</h1>");

    }

    @Override

    public void destroy(){

        

        System.out.print("Done");

    }

 

}

OUTPUT:

Programs based on Servlet

b.Develop a servlet web application to create a login form which take input -Name, Phone number, Address, Language known(i.e., Hindi, Marathi, Kannada etc.) and hobbies(Reading, Travelling, Singing etc). Show the Entered values.

Index.html

<!DOCTYPE html>

<html>

    <head>

        <title>Servlet practical2</title>

        <meta charset="UTF-8">

        <meta name="viewport" content="width=device-width, initial-scale=1.0">

    </head>

    <body>

        <form name ="frm" method = "post" action = "servletprac2">

            <table border="1">

                <thead>

                    <tr>

                        <th colspan="2"><h2>Enter Your Details:-</h2></th>

                    </tr>

                </thead>

                <tbody>

                    <tr>

                        <td>Enter Your Name:</td>

                        <td><input type = "text" name = "txt1"/></td>

                    </tr>

                    <tr>

                        <td>Enter Your Mobile No.</td>

                        <td><input type = "text" name="txt2"/></td>

                    </tr>

                    <tr>

                        <td>Enter Your Address:</td>

                        <td><textarea rows ="8" cols="30" name = "addr"></textarea></td>

                    </tr>

                    <tr>

                        <td><label>Language Known:</label></td>

                        <td>

                            <input type ="checkbox" name="ch1" value="English"/>English 

                            <input type ="checkbox" name="ch1" value="Hindi"/>Hindi 

                            <input type ="checkbox" name="ch1" value="Marathi"/>Marathi 

                            <input type ="checkbox" name="ch1" value="Tamil"/>Tamil 

                            <input type ="checkbox" name="ch1" value="Kannada"/>Kannada 

                        </td>

                    </tr>

                    <tr>

                        <td><label>Hobbies:</label></td>

                        <td>

                            <select name="hob" multiple="multiple">

                                <option>Reading</option>

                                <option>Traveling</option>

                                <option>Swimming</option>

                                <option>Singing</option>

                            </select>

                        </td>

                    </tr>

                    <tr>

                        <td colspan="2"><center><input type="submit" name="btn1" value="Submit"/></center></td>

                    </tr>

                </tbody>

            </table>

 

        </form>

    </body>

</html>

servletprac2.java

package pack2;

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 servletprac2 extends HttpServlet {

 

    @Override

    protected void doPost(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        

        response.setContentType("text/html;charset=UTF-8");

        PrintWriter out = response.getWriter();

        String name = request.getParameter("txt1");

        String num = request.getParameter("txt2");

        String add = request.getParameter("addr");

        String lang[] = request.getParameterValues("ch1");

        String hobi[] = request.getParameterValues("hob");

        out.println("<h1>Entered values are</h1>");

        out.println("<h2>Applicant Name:</h2>"+name);

        out.println("<h2>Applicant address:</h2>"+add);

        out.println("<h2>Applicant Mobile Number:</h2>"+num);

        out.println("<h2>Langueges Known:</h2><h2>");

        for(int i = 0;i<lang.length;i++){

            out.print(lang[i]+"\t");

        }

        out.println("</h2>");

        out.println("<h2>Hobbies</h2><h2>");

        for(int i = 0;i<hobi.length;i++){

            out.print(hobi[i]+"\t");

        

        }

        out.println("</h2>");  

    }

}

 


OUTPUT:

Programs based on Servlet


Programs based on Servlet

 c.Write a servlet program to find prime number and factorial. Create error page for factorial if negative number is entered as input.

Index.html
 
<html>
    <head>
        <title>Welcome</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <h1>Select below</h1>
        <a href="factorial.html">1.Factorial</a><br>
        <a href="prime.html">2.Prime</a>
    </body>
</html>
 
prime.html
<html>
    <head>
        <title>Prime</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <form name ="frm2" method="post" action="prime">
            <b>Enter an integer Number:</b>
            <input type="text" name="n2"/><br>
            <input type="submit" value="click"/>
        </form>
    </body>
</html>
 
prime.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class prime extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset = UTF-8");
        PrintWriter out = response.getWriter();
        try{
            int n = Integer.parseInt(request.getParameter("n2"));
            int r = 0;
            for(int i = 1; i <= n;i++){
                if (n%i == 0){
                    r++;
                }
            }
            if (r == 2){
                out.print("<h2> "+n+" is a prime number </h2>");
            }
            else{
                out.print("<h2> "+n+" is not a prime number </h2>");
            }
        }
        catch(Exception e){
        }  
    }
}
 
factorial.html
<html>
    <head>
        <title>Factorial</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <form name ="frm1" method="post" action="Fact">
            <b>Enter an integer Number:</b>
            <input type="text" name="n1"/><br>
            <input type="submit" value="Submit"/>
        </form>
    </body>
</html>
 
Fact.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Fact extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset = UTF-8");
        PrintWriter out = response.getWriter();
        try{
            int n = Integer.parseInt(request.getParameter("n1"));
            if(n>=0){
                if (n == 0){
                    out.print("<h2>Factorial of 0 is 1</h2>");
                }
                else{
                    int f = 1;
                    for(int i = 1; i <= n; i++){
                        f = f*i;
                    }
                    out.print("<h2>The factorial of "+n+" is "+f+" </h2>");
                }
            }
            else{
                response.sendRedirect("error.html");
            }
        }
        catch(Exception e){
            
        }
    }
}
 
error.html
<html>
    <head>
        <title>Error</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        Enter a positive number...<br/>
        <b>Go back to</b>
        <a href="factorial.html">Factorial</a>
    </body>
</html>

output:

Programs based on Servlet


Programs based on Servlet

Programs based on Servlet

Programs based on Servlet

Programs based on Servlet











Post a Comment

0 Comments