Hi,
i'm implementing the model-view-controller model and am trying to get my controller servlet to display a default login jsp page on startup, i'm doing this by placing a call to this page in the servlets service() method, from this page which displays a simple form i call the servlets doPost() method and redirect to a different page, but this method doesn't get called and when i click the submit button the login page is redisplayded. I think this is because the service() method is called before doPost or doGet? Any suggestions on how to get a servlet to display a default page on startup or when the servlet is called and then display another based on an action in that page?
The code for my jsp and servlet is as follows:
And the code for my servlet is:
i'm implementing the model-view-controller model and am trying to get my controller servlet to display a default login jsp page on startup, i'm doing this by placing a call to this page in the servlets service() method, from this page which displays a simple form i call the servlets doPost() method and redirect to a different page, but this method doesn't get called and when i click the submit button the login page is redisplayded. I think this is because the service() method is called before doPost or doGet? Any suggestions on how to get a servlet to display a default page on startup or when the servlet is called and then display another based on an action in that page?
The code for my jsp and servlet is as follows:
Code:
<HTML>
<HEAD>
<TITLE>Welcome to the login page</TITLE>
</HEAD>
<BODY>
<h1>Please Enter Your Login Details</h1>
<form name="login" action="LoginServlet" method="post">
Name: <input type="text" name="name"><br>
Password:<input type="password" name="pwd"><br>
<input type="submit" name="Login" value="Login"><br>
<input type="hidden" name="action" value="LOGIN">
</form>
</BODY>
</HTML>
And the code for my servlet is:
Code:
import javax.servlet.ServletException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*@version SurfProjectLogin.java 1.0 04/02/22
*@author james
*/
public class LoginServlet extends HttpServlet {
private DBConn Dbc;
/**
*init method initialises Dbc class variable and sets up a database connection
*/
public void init(){
Dbc = new DBConn();
}
/**
*service method tries to show the Login page on startup
*/
public void service( HttpServletRequest req, HttpServletResponse res) throws ServletException, java.io.IOException{
gotoPage("/Login.jsp",req, res);
}
/**
*doGet method
*/
public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException, java.io.IOException {
System.out.println("hello from doget()");
}
/**
*doPost method
*/
public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException, java.io.IOException {
boolean isValidUser=false;
java.sql.ResultSet rs;
String password=req.getParameter("pwd");
rs = Dbc.query("select Password from SurfEmployees");
try{
while (rs.next()) {
String s = rs.getString("Password");
if (password.equals(s))
isValidUser=true;
}
if (isValidUser == true)
gotoPage("/Results.jsp",req,res);
else
gotoPage("/Error.jsp",req,res);
}catch(java.sql.SQLException sqle){
getServletContext().log("ERROR WHILE SEARCHING THRO THE RESULT SET");
}
}
/**
*sends the user to a specified page
*@param address where the page is
*@param req Request obj
*@param res Response obj
*/
private void gotoPage(String address, HttpServletRequest req,HttpServletResponse res) throws ServletException, java.io.IOException{
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(address);
dispatcher.forward(req, res);
}
/**
*destroy method closes the database connection
*/
public void destroy(){
Dbc.closeDBConn();
Dbc=null;
}
}