2
Answers

Login and Register form in one page in servlet

Venkatesh K H

Venkatesh K H

7y
259
1
I have stored email, dob, first name and last name in the database. Now i want to make password creation. In login page first time user should create password and it should store in the db. Again if the same user logs in with correct credentials, it should redirect to landing page. But there are only two fields in font end one is email and another one is password.
 
 

 pasting

			

package admin.com;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.sql.*;

import javax.servlet.http.HttpSession;

public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 102831973239L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LoginServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		String email = request.getParameter("useremail");
		String password = request.getParameter("password");
		
		

		
		String searchQuery = "select * from login where email='" + email
				+ "' AND password='" + password + "'" ;
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			System.out.println(e.getMessage());
		}
		try {
			Connection con = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/test", "root", "" + ""
							+ "" + "");
			Statement stmt = con.createStatement();
			ResultSet rs = stmt.executeQuery(searchQuery);
			
			
			 
			// out.println("email: "+email);
			// out.println("password: "+password);
			// out.println("type: "+type);
		//	out.println("type: ");
			
			boolean isEmpty = rs.next();
		
		//	out.println("type: " + type);
			if (!isEmpty) {
				// redirect to error page
				// System.out.println("<font color=red>invalid user name or password</font>");
				//request.getSession().removeAttribute("errorMessage");
				
			//	System.out.println("Invalid Credentials");
				
				response.sendRedirect("index.jsp");
				request.setAttribute("error","Invalid Username or Password");
			
			} else if (isEmpty) {
				// fetch the session from request, create new session if session
				// is not present in the request
				HttpSession session = request.getSession(true);
				session.setAttribute("FirstName", rs.getString("first_name"));
			    session.setAttribute("LastName", rs.getString("last_name"));
				session.setAttribute("employee_id", rs.getString("employee_id"));
				
				// session.setAttribute("Type", rs.getString("type"));
				// redirect to success page
			  //	session.setMaxInactiveInterval(50); // *600 secs = 10 mins *//

				// if(rs.next()){
				// stype t1=new type();
				String user_type = rs.getString("user_type");
				// String type= rs.getString("type");
			//	out.println("type: " + type);
				if ("admin".equals(user_type)) {
					// redirect to buyer page
					response.sendRedirect("Landing.jsp");
				} else if ("emp".equals(user_type)) {
					// redirect to seller page
					response.sendRedirect("error.jsp");
					request.setAttribute("error","Invalid Username or Password");
					
				}

				// }

			}
		} catch (SQLException e) {
			System.out.println("SQLException occured: " + e.getMessage());
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
Answers (2)