Gadgets

Showing posts with label jsp. Show all posts
Showing posts with label jsp. Show all posts

Monday, 26 November 2012

Login form using jsp and servlet with sessions

Login form using jsp and servlet
this code is developed in netbeans
the RequestDispatcher interface provides the facility of dispaching the request to another resource it may html, jsp, and servlet
code for jsp :
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
       
        <title>user registration</title>
        <style type="text/css">
            body{
                margin-top: 100px;
              
            }
            #tf,select{
             border: solid 1px darkcyan; 
            }
            h2{
                color:  cornflowerblue;
                font-family: cursive;
            }
            #button{
                color: white;
                background-color:blue;
                border: sloid 1px #aaaaaa;
                cursor: pointer;
                width: 100px;
                height: 30px;
               
        
            }
        </style>
    </head>
    <body>
    <center>
        <h2>Sing In Here</h2>
        <form action="LoginServlet" method="post">
            <table>
               
                <tr>
                    <td>Email id </td><td><input type="text" name="mailid" value=""id="tf" /></td>
                </tr>
                <tr>
                    <td>Password </td><td><input type="password" name="psw" value="" id="tf"/></td>
                </tr>
              
                <tr><td></td><td><input type="submit" name=""value="Sign In" id="button"/></td></tr>
            </table>
        </form>
    </center>
    </body>
</html>
Servlet code :
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
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 {
        protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, ClassNotFoundException, SQLException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String mid,psw;
            mid=request.getParameter("mailid");
            psw=request.getParameter("psw");
            Class.forName("com.mysql.jdbc.Driver");
            Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/myblog","narendar_blog","12345");
            String q="select * from account_info where mail_id='"+mid+"' and password='"+psw+"'";
            Statement st=con.createStatement();
            ResultSet rs=st.executeQuery(q);
            String tmid ="",tpsw="",tname="";
            while(rs.next()){
                tmid=rs.getString("mail_id");
                tpsw=rs.getString("password");
                tname=rs.getString("name");
            }
            if(mid.equals(tmid)&&psw.equals(tpsw)){
                HttpSession session=request.getSession(true);
                        session.setAttribute("mail_id", mid);
                        request.setAttribute("mail_id",mid);
                        request.setAttribute("name",tname);
              RequestDispatcher rd=request.getRequestDispatcher("account_home.jsp");
              rd.forward(request, response);
            }
        }
        catch(Exception e)
        {
            out.println(e);
        }finally {           
            out.close();
        }
    }