Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Login Servlet not responding
- <html>
- <body>
- <form action="loginservlet" method="post">
- <h1>Please Login</h1>
- Login: <input type="text" name="username"><br>
- Password: <input type="password" name="password"><br>
- <input type=submit value="Login">
- </form>
- </body>
- </html>
- package connectm;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.sql.*;
- 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 {
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // TODO Auto-generated method stub
- response.setContentType("text/html");
- PrintWriter out = response.getWriter();
- Connection conn = null;
- String url = "jdbc:mysql://localhost:3306/";
- String dbName = "test";
- String driver = "com.mysql.jdbc.Driver";
- String userName = "root";
- String password = "root";
- String username="";
- String userpass="";
- String strQuery="";
- Statement st=null;
- ResultSet rs=null;
- HttpSession session = request.getSession(true);
- try {
- Class.forName(driver).newInstance();
- conn = DriverManager.getConnection(url + dbName, userName,
- password);
- if (request.getParameter("username") != null
- && request.getParameter("username") != ""
- && request.getParameter("password") != null
- && request.getParameter("password") != "") {
- username = request.getParameter("username").toString();
- userpass = request.getParameter("password").toString();
- /*strQuery = "select * from user where username='" + username
- + "' and password='" + userpass + "'";*/
- //System.out.println(strQuery);
- strQuery = "select username from user where username= ? and password= ?";
- st = conn.createStatement();
- PreparedStatement ps=conn.prepareStatement(strQuery);
- ps.setString(1, username);
- ps.setString(2, userpass);
- rs = ps.executeQuery(strQuery);
- int count = 0;
- while (rs.next()) {
- session.setAttribute("username", rs.getString(1));
- count++;
- }
- if (count > 0) {
- response.sendRedirect("welcome.jsp");
- } else {
- response.sendRedirect("login.jsp");
- }
- } else {
- response.sendRedirect("login.jsp");
- }
- out.println("Connected to the database");
- conn.close();
- out.println("Disconnected from database");
- } catch (Exception e) {
- // TODO: handle exception
- }
- }
- }
- } catch (Exception e) {
- // TODO: handle exception
- }
- } catch (Exception e) {
- throw new ServletException("Login failed", e);
- }
Add Comment
Please, Sign In to add comment