Reputation: 2571
I have a jsp file that provides the login screen, upon submitting the form the control has to go to a servlet. Now, how can i save the values in the form to a model(Bean classs) and get them use in the controller.? I am not using any frameworks like struts, spring, etc.
I used the following code but getting the error
java.lang.NoClassDefFoundError: bean/LoginBean
My code is:
index.jsp:
<form name="signin" method="post" action="LoginServlet">
<table>
<tr><td><font>USERNAME</font></td><td><input type="text" name="signin_uname" /></td></tr>
<tr><td><font>PASSWORD</font></td><td><input type="password" name="signin_pwd" /></td></tr>
<tr><td><input type="reset" value="RESET" /></td><td><input type="submit" value="LOGIN" /></td></tr>
</table>
</form>
LoginServlet.java
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
HttpSession session=request.getSession();
System.out.println(session);
try {
LoginBean login=new LoginBean();
login.setSignin_pwd("raviteja");
login.setSignin_uname("raviteja");
System.out.println(login.getSignin_uname());
System.out.println(login.getSignin_pwd());
} finally {
}
response.sendRedirect("");
}
LoginBean.java
public class LoginBean implements Serializable {
String signin_uname,signin_pwd;
public LoginBean() {
}
public String getSignin_pwd() {
return signin_pwd;
}
public void setSignin_pwd(String signin_pwd) {
this.signin_pwd = signin_pwd;
}
public String getSignin_uname() {
return signin_uname;
}
public void setSignin_uname(String signin_uname) {
this.signin_uname = signin_uname;
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>servlets.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
Upvotes: 0
Views: 632
Reputation: 3671
Your code should run properly. You've a problem with the classpath. JVM isn't able to find the class LoginBean
at runtime.
By the way what you want to output to the console? If you want to print the entered data user then correct your code with the following:
LoginBean login = new LoginBean();
String username = request.getParameter("signin_uname");
String password = request.getParameter("signin_pwd");
login.setSignin_uname(username);
login.setSignin_pwd(password);
But error shouldn't be if you correctly have created a project with your code.
Upvotes: 0
Reputation: 5220
1 Add Spring libraries to your project and map DispatherServlet in web.xml to process /LoginServlet
2 Create bean (same names for form and bean fields):
public class LoginData {
private String signin_uname;
private String signin_pwd;
// Getters and setters
}
3 Create controller:
@Controller
public class LoginController {
@RequestMapping(value = "/LoginServlet", method = RequestMethod.POST)
public String postLoginData(@ModelAttribute LoginData loginData) {
// All data from form will be at your model attribute bean. It will also will
// be putted at request
String userName = logigData.getSignin_uname();
return "loginResult.jsp";
}
}
Upvotes: 1