Tuesday 4 April 2017

Develop a simple JSP program for user registation and then control will be transfer it into second page.

*First

     -index.jsp:registation form
     -first.jsp: second Control of JSP.

*Code

-index.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="first.jsp" method="post">
            Enter your UserName:<input type="text" name="name"><br>
            Enter your Password:<input type="password" name="pas"><br>
            Enter your email:<input type="email" name="email"><br>
          
            <input type="submit">
        </form>
       
    </body>
</html>
 



-First.jsp

<%--
    Document   : first.jsp
    Created on : Apr 4, 2017, 8:45:35 PM
    Author     : Queue
--%>

<%@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>
      
        <%
           String name=request.getParameter("name");
           String password=request.getParameter("pas");
           String email=request.getParameter("email");
          
            out.println("You Entered Name :"+name+"<br>");
            out.println("You Entered pas :"+password+"<br>");
            out.println("You Entered email :"+email+"<br>");
        %>
    </body>
</html>



-OUTPUT






Thanks ;)

Happy Coding






 
 

Monday 3 April 2017

Develop jsp program to display the grade of a student by accepting the marks of five subject

*File Name

        - Marks.html :Insert five subject marks.
        - marks.jsp: obtaining grade 

*Code

-marks.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>TODO supply a title</title>
       
    </head>
    <body>
    <center>
        <h1>Student marks</h1>
        <form action="marks.jsp" method="get">
           Enter Makrs in Java : <input type="text" name="java"> <br><br>
            Enter NMA Marks : <input type="text"  name="NMA"><br><br>
            Enter MCAD Marks :<input type="text"  name="MCAD"><br><br>
            Enter PPUD Marks :<input type="text"  name="PPUD"><br><br>
            Enter Project Marks :<input type="text"  name="pro"><br><br>
            <input type="submit">
           </form>
        </center>
    </body>
</html>


*Marks.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 java=Integer.parseInt(request.getParameter("java"));
             int NMA=Integer.parseInt(request.getParameter("NMA"));
             int MCAD=Integer.parseInt(request.getParameter("MCAD"));
             int PPUD=Integer.parseInt(request.getParameter("PPUD"));
             int Project=Integer.parseInt(request.getParameter("pro"));
              
            int c=java+NMA+MCAD+PPUD+Project;
            double avg=c/5;
        
           
         if(avg > 90 )
         {
             out.println(" your grade is A");
         }else if (avg >= 80) {
           out.println("your grade is b");
        } else if (avg >= 70) {
            out.println("your grade is c");
        } else if (avg >= 60) {
             out.println("your grade is d");
        } else {
             out.println("your grade is e");
       }
       %>
    </body>
</html>



OUTPUT





Thanks ;)

Happy Coding 

 









 






 
 

Develop a simple JSP Program for User login Form With static and Dynamic database.

*Static Database

*FileName.
        
       -Index.html : For UserName & Password.
       -Process.jsp: For Static Database.
       -Welcome.jsp :Welcome File

*Code

Index.html



    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <form action="Process.jsp" method="post">
        Username:<input type="text" name="uname">
        password:<input type="password" name="pass">
        <input type="submit">
        </form>
       
    </body>
</html>


Process.jsp


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

<!DOCTYPE html>

<%

     String a=request.getParameter("uname");

     String b=request.getParameter("pass");

 if(a.equals("queue") && b.equals("queue"))

       {
             response.sendRedirect("Welcome.jsp");

       }

 %>  

Welcome.jsp


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%
    out.println("Welcome to world of jsp");
    %>


*Output


 

*Dynamic Database

*FileName
   
              -index.jsp :For Register Form.

              -servlet.jsp: Insert data indata base & redirect page                            login.jsp

              -login.jsp : Make sure Login Data.

              -wel.jsp:Welcome File. 


*Code

*index.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="servlet.jsp" method="post">
            Enter your UserName:<input type="text" name="name"><br>
            Enter your Password:<input type="password" name="pas"><br>
            Enter your email:<input type="email" name="email"><br>
          
            <input type="submit">
        </form>
       
    </body>
</html>

 
 

*servlet.jsp

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

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <%
              String name=request.getParameter("name");
              String pass=request.getParameter("pas");
              String email=request.getParameter("email");
            
             
           
              Class.forName("com.mysql.jdbc.Driver"); 
              Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/phgraph","root","ujash7878");
              PreparedStatement ps=con.prepareStatement("insert into reg values(?,?,?)");
             
ps.setString(1,name);
ps.setString(2, pass);
ps.setString(3, email);

 int i=ps.executeUpdate();

if(i>0)
{
    response.sendRedirect("login.jsp");
}
else
{
    out.println("Try Again");
}

       
       
        %>
    </head>
    <body>
</body>
</html>


*login.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="wel.jsp" method="post">
            Name:<input type="text" name="name"><br>
            password:<input type="password" name="pas"><br>
            <input type="submit">
        </form>
      
       
    </body>
</html>
 

*Wel.jsp

<%@page import="java.io.*"%>
<%@page import="java.util.*" %>
<%@page import="java.sql.*" %>
<%@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>
        <%
            String name=request.getParameter("name");
              String pass=request.getParameter("pas");
             
 Class.forName("com.mysql.jdbc.Driver"); 
              Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/phgraph","root","ujash7878");
              PreparedStatement ps=con.prepareStatement("select * from reg where name=? and pas=?");
              ps.setString(1, name);
              ps.setString(2, pass);
             
   ResultSet rs=ps.executeQuery();
     
       if(rs.next())
       {
           out.println("<h1>Welcome :"+name+"</h1>");
       }
       else
       {
           out.println("<h1>Not Done</h1>");
       }

       %>
     </body>
</html>


 *OUTPUT

           


 



 

  
Thanks ;)

Happy Coding.



 

 

 

  
 
    

 

Saturday 1 April 2017

Login & logout With use of HttpSession

*File Name
        
        1)Web.xml : Welcome file of Servlet.  
        2) index.html: Which is indicate other File With link.
        3)login.html:For Login Name & login Password.
        4)logingServlet:For Create Session.
        5)logoutServlet:for Loutout Session. 
        6)ProfileServlet:See Session ProfileView.


*Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">
    <servlet>
        <servlet-name>loginServlet</servlet-name>
        <servlet-class>loginServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>loginServlet</servlet-name>
        <url-pattern>/loginServlet</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>logoutServlet</servlet-name>
        <servlet-class>logoutServlet</servlet-class>
           
    </servlet>
    <servlet-mapping>
        <servlet-name>logoutServlet</servlet-name>
        <url-pattern>/logoutServlet</url-pattern>
    </servlet-mapping>
     <servlet>
        <servlet-name>ProfileServlet</servlet-name>
        <servlet-class>ProfileServlet</servlet-class>
           
    </servlet>
    <servlet-mapping>
        <servlet-name>ProfileServlet</servlet-name>
        <url-pattern>/ProfileServlet</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>



 *index.html

<html>
<body>
        <a href="login.html">Login</a>||
        <a href="logoutServlet">Logout</a>||
        <a href="ProfileServlet">Profile</a>
    </body>
</html> 



*login.html


<html>
<body>
        <form action="loginServlet" method="post">
            Name:<input type="text" name="name"><br>
            password:<input type="password" name="pas"><br>
            <input type="submit">
        </form>
    </body>
</html>



*loginServlet




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;
import javax.servlet.http.HttpSession;


public class loginServlet extends HttpServlet {


 @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {
      
         res.setContentType("text/html");
        PrintWriter out=res.getWriter();
      
        String Name=req.getParameter("name");
        String password=req.getParameter("pas");
      
      
        req.getRequestDispatcher("index.html").include(req, res);
      
        if(password.equals("ujash"))
        {
            HttpSession session=req.getSession();
            session.setAttribute("Name", Name);
            out.println("<br>Welcome :"+Name);
          
        }
        else
                {
                 out.println("You Have entered Wrong Password");
                }
    }
    
    }



*logoutServlet.java



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;
import javax.servlet.http.HttpSession;


public class logoutServlet extends HttpServlet {
   
   
    public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException, ServletException
    {
        res.setContentType("text/html");
        PrintWriter out=res.getWriter();
       
        req.getRequestDispatcher("login.html").include(req, res);
       
        HttpSession session=req.getSession();
        session.invalidate();
       
        out.println("<br>You have successfully Logout");
      }
   
}
 


*ProfileServlet.java



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;
import javax.servlet.http.HttpSession;


public class ProfileServlet extends HttpServlet {
   
    public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException, ServletException
    {
       
        res.setContentType("text/html");
        PrintWriter out=res.getWriter();
       
        req.getRequestDispatcher("index.html").include(req, res);
               
       
        HttpSession session=req.getSession(false);
       
        if(session!=null)
        {
            String Name=(String) session.getAttribute("Name");
           
            out.println("<br> Welcome Again :" +Name);
        }
        else
        {
            req.getRequestDispatcher("index.html").include(req, res);
            out.println("<br> Plz Login First");
            req.getRequestDispatcher("login.html").include(req, res);
        }
       
       
    }
   
   
}



*outPut


 
 




 
Happy Coding

Thanks ;)