Issue
Login Controller:
package test;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.webappmvc.mvc.controller.Controller;
public class Login implements Controller{
public void doProcess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
System.out.println("UserName Parameter Value: " + request.getParameter("username"));
if(request.getParameter("login") != null && request.getParameter("userName") != null && request.getParameter("password") != null){
if(!request.getParameter("userName").equals("") && !request.getParameter("password").equals("") && request.getParameter("login").equals("Login")){
ArrayList<String> data = (ArrayList<String>) request.getAttribute("model");
for(int i=0; i<data.size(); i+=2){
System.out.println("Username from DB: " + data.get(i) + "\nPassword: " + data.get(i+1));
// if(request.getParameter("username").toString().equals(data.get(i))){
// if(request.getParameter("password").toString().equals(data.get(i+1).toString())){
// session.setAttribute("loggedUser", data.get(i).toString());
// response.sendRedirect("home");
// return;
// }
// request.setAttribute("errorMsg", "Invalid Login");
// }
}
}
}
System.out.println("Result view is: " + request.getAttribute("view"));
request.getRequestDispatcher(request.getAttribute("view").toString()).forward(request, response);
}
public String addMapping() {
return "/login";
}
}
Here, Every request going through WebappController class defined in com.webappmvc.mvc.controller.Controller, com.webappmvc.mvc.controller.Controller#doGet method select controller, model, view on runtime, then forward it to com.webappmvc.mvc.controller.ResponseController#doGet Then, In response Controller, some code like this which forward request, response client project Controller#doProcess method.
Controller controller = (Controller) request.getAttribute("controller");
controller.doProcess(request, response);
With these, when i tried to acces /login, LoginController getting called, But getParameter() returning null each and every time. I also tried to acces this URL
Requested URL: http://localhost:8080/FreameworkTest/controller/login?userName=test&password=test&login=Login
I'm Getting Output in console like these.
Console Output:
All request passing through WebApp Controller //From WebAppController
----- From Response Controller //From WebAppResponseController
UserName Parameter Value: null //From LoginController
Username from DB: jitu
Password: 12345
Result view is: /WEB-INF/views/Login.jsp
Solution
You are using send :
userName=test
And get the attribute with
request.getParameter("username");
You made a typo. Use instead
request.getParameter("userName");
Error you didn't do in the second part.
NOTE:
I suggest you define constant value for those instead of use a String
literal
private final statis String USER_NAME = "userName";
And use it each time you need
request.getParameter(USER_NAME);
Answered By - AxelH