0

i have created a jsp page view.jsp and corresponding to that i have created a servlet admin.java code of both below... when i click on register a blank page appears...redirects are not working. please help me in resolving this

view.jsp

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"               

    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    <form action="admin">
    username <input type="text" name="username" value="" />
    password <input type="text" name="password" value="" />
    <input type="submit" name="register" value="register" />"
    </form>
    </body>
    </html>

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                admin.java



 import java.io.*;


    import java.sql.*;


    import javax.servlet.*;
    import javax.servlet.http.*;


    public class admin extends HttpServlet {

    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://localhost:3306/inventory";
    static final String USER = "root";
    static final String PASS = "root";


    Connection conn = null;
    Statement stmt = null;

    public void doGet(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {



    try {

    String user = req.getParameter("username");
    String pass = req.getParameter("password");


    Class.forName("com.mysql.jdbc.Driver");
    conn = DriverManager.getConnection(DB_URL, USER, PASS);
    stmt = conn.createStatement();
    String sql = "select * from admins";
    //String sql = "select * from admins WHERE username='"+user+"' AND     

    password='"+pass+"'";
    ResultSet rs = stmt.executeQuery(sql);


    PrintWriter pw = res.getWriter();
    //res.setContentType("text/html");



    while (rs.next()) 
    {
    if((user.equals(rs.getString(0))) &&  (pass.equals(rs.getString(1))))
    {
    //String n=rs.getString("username");
    //String p=rs.getString("password");
    res.sendRedirect("loginsuccess.jsp");

    }
    else

    {
    res.sendRedirect("loginfailure.jsp");
    }
    }

    pw.close();

    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    }

    }
Santhosh
  • 8,181
  • 4
  • 29
  • 56

4 Answers4

0

Use RequestDispatcher instead of sendRedirect():

RequestDispatcher reqDispatcher = req.getRequestDispatcher("path/loginsuccess.jsp");
            reqDispatcher.forward(req, res);

Read more about RequestDispatcher.

Read the difference between RequestDispatcher and sendRedirect, and whento use both of them.

Community
  • 1
  • 1
Salah
  • 8,567
  • 3
  • 26
  • 43
0

Try this

getServletContext().getRequestDispatcher("/loginsuccess.jsp").forward(request, response);
Santino 'Sonny' Corleone
  • 1,735
  • 5
  • 25
  • 52
0

sample code :

User user = userDAO.find(username, password);
if (user != null) {
    request.getSession().setAttribute("user", user); // Login user.
    response.sendRedirect("home"); // Redirects to http://example.com/context/home after succesful login.
} else {
    request.setAttribute("error", "Unknown login, please try again."); // Set error.
    request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response); // Forward to same page so that you can display error.
}

you should try that method, but my suggestion as:

  1. I think you should not close the statement, result set and connection,.

  2. And then you should check the if condition. if maybe your condition have an error the page like empty. so should check that..

source from : https://stackoverflow.com/a/2048640/3242978

Community
  • 1
  • 1
jmail
  • 5,944
  • 3
  • 21
  • 35
0

When you click 'register' button it submits the form as a HTTP POST request. You need to implement doPost() instead.

isak gilbert
  • 2,541
  • 1
  • 14
  • 11