Programs based on JSP.

Write JSP files that accepts a number from the HTML form

         (i):Display whether it’s even or odd

         (ii):It’s Factorial

         (iii):Multiplication table

Index.html

<html>

    <head>

        <title>TODO supply a title</title>

        <meta charset="UTF-8">

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

    </head>

    <body>

        <form name="frm1" action="Display.jsp" method="POST">

            <label>Enter an integer number:</label><input type="text" name="num1"/><br/>

            <input type="submit"/>

        </form>

    </body>

</html>

 

Display.jsp

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

<!DOCTYPE html>

<html>

    <head>

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

        <title>JSP Page</title>

    </head>

    <body>

        <%

            int n=Integer.parseInt(request.getParameter("num1"));

            if(n%2==0)

                out.println("<h2>"+n+" is even number </h2>");

            else

                out.println("<h2>"+n+" is odd number </h2>");

            int f=1;

            for(int i=1;i<=n;i++)

                f=f*i;

            out.println("<h2>Factorial of "+n+ " is "+f+"</h2>");

            out.println("<h2>Multiplication table of "+n +" is as follows</h2>");

            for(int j=1;j<=10;j++)

                out.println("<h3>"+n+"*"+j+"="+n*j+"</h3>");

        %>

    </body>

</html>

 

OUTPUT:

Programs based on JSP.

Programs based on JSP.

Write a JSP page to accept two numbers(m, n) from the user, on click of the submit button display on a new JSP page of all prime numbers between m and n. If the number m is not less than n(m<n), it should redirect to a different error page having link to the JSP form.

Index.html

<html>

    <head>

        <title>TODO supply a title</title>

        <meta charset="UTF-8">

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

    </head>

    <body>

        <form name="form" action="Primeno.jsp" method="post">

            <label>Enter an integer number</label><input type="text" name="txt1"/><br/>

            <label>Enter an integer number</label><input type="text" name="txt2"/><br/>

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

        </form>

    </body>

</html>

 

Primeno.jsp

 

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

<%@page errorPage="Error.jsp" %>

<!DOCTYPE html>

<html>

    <head>

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

        <title>JSP Page</title>

    </head>

    <body>

        <%

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

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

         int m=Integer.parseInt(str1);

         int n=Integer.parseInt(str2);

         %>

        <h1>Prime numbers from <%=m%> to <%=n%> are as follows</h1>

        <%

            if(m<n)

            {

                int f=1;

                for(int i=m;i<=n;i++)

                {

                    for(int j=2;j<i;j++)

                    {

                        if(i%j==0)

                        {    

                            f=0;

                            break;

                        }    

                    }

                    if(f==1)

                        out.print(i+"\t");

                    f=1;

                }

            }

            else

            {

                response.sendRedirect("Error.jsp");  

            }   

        %>

        <br/><br/><br/><a href="index.html">Click here to go to Home page</a>

    </body>

</html>

 

Error.jsp

 

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

<%@page isErrorPage="true" %>

<!DOCTYPE html>

<html>

    <head>

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

        <title>JSP Page</title>

    </head>

    <body>

        <h3>Sorry an exception occured!</h3>

        Exception is: <%= exception %><br/>

        <a href="index.html">Click here to go to Home page</a>

    </body>

</html>

 

Input.jsp

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

<!DOCTYPE html>

<html>

    <head>

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

        <title>JSP Page</title>

    </head>

    <body>

        <form action="Primeno.jsp" method="post">

            <label>Enter an integer number</label><input type="text" name="txt1"/><br/>

            <label>Enter an integer number</label><input type="text" name="txt2"/><br/>

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

        </form>

    </body>

</html>

OUTPUT:

Programs based on JSP.

Programs based on JSP.

Programs based on JSP.

Write a JSp page which dipslays three text boxes for user name, password and email.      On click of submit button it should call another JSP page which will store the values in

The database with the help of preparedstatement class.Also use jspInit() and jspDestroy() to open and close connection.

CODE:-

 

index.htmlTop of Form

 

Bottom of Form

<html>

    <head>

        <title>JSP</title>

        <meta charset="UTF-8">

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

    </head>

    <body>

        <form name="frm2" method="post" action="store.jsp">

            <center><table border="1">

                <thead>

                    <tr>

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

                    </tr>

                </thead>

                <tbody>

                    <tr>

                        <td>Name:</td>

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

                    </tr>

                    <tr>

                        <td>E-mail:</td>

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

                    </tr>

                    <tr>

                        <td>Password:</td>

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

                    </tr>

                    <tr>

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

                    </tr>

                </tbody>

                </table></center>

        </form>

    </body>

</html>

 

 

 

Store.jspTop of Form

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

<%@page import="java.sql.*;" %>

 

<html>

    <head>

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

        <title>JSP</title>

    </head>

    <body>

        <%!

            Connection con;

            Statement st;

            PreparedStatement ps;

            ResultSet rs;

            ResultSetMetaData rsmd;

            int col;

            public void jspInit()

            {

                try{

                    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

                }

                catch(ClassNotFoundException e){

                    System.out.println(e);

                }

            }

        %>

        <%

            try{

                con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=kiran","sa","123456");

                st = con.createStatement();

                String str1 = request.getParameter("txt");

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

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

                ps = con.prepareStatement("insert into register values(?,?,?)");

                ps.setString(1, str1);

                ps.setString(2, str2);

                ps.setString(3, str3);

                ps.executeUpdate();

                rs =st.executeQuery("select * from register");

                rsmd=rs.getMetaData();

                col = rsmd.getColumnCount();

                

                

            }

            catch(Exception e)

            {

                

            }

            for(int i=1;i<col+1;i++){

                out.println("<b>"+rsmd.getColumnName(i)+"    </b>");

                

            }

            out.println("<br>");

            while(rs.next()){

                out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3)+"<br>");

            }

        %>

        <%!

            public void jspDestroy(){

                try{

                    ps.close();

                }

                catch(SQLException e){

                    System.out.println(e);

                }

            }

        %>

    </body>

</html>

output:

Programs based on JSP.

Programs based on JSP.

Post a Comment

0 Comments