ECM
ECM
总版主
总版主
  • 社区居民
  • 忠实会员
  • 原创写手
阅读:826回复:0

How to save data in variables defined in Java class using servlet and JSP?

楼主#
更多 发布于:2016-05-15 13:58
I have created a maven project in STS. In project i have 1 jsp file and 1 servlet. jsp asks user to enter user and password, and then servlet get values, displays them in HTML format. But i have 1 java class too. I want the entered data in jsp file to be stored in 2 variables defined in java class but i am unable to do so. I have tried few things but unable to understand how to do it. HelloServlet: public class HelloServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;

public HelloServlet() {
    super();

protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {

    PrintWriter out = response.getWriter();
    out.println("<title>My Intro Servlet!</title>");

    out.println("<h1>Welcome " + request.getParameter("name") + " "
            + "Password: " + request.getParameter("Password") + "!</h1>");
    out.println("<p><a href = http://localhost:9080/HelloWorldWeb/Hello.jsp>"
            + "<input type = submit value = Back /></a></p>");

    String User = request.getParameter("Name");
    String Password = request.getParameter("Password");

    User user1 = new User();

    user1.setUser(request.getParameter("name"));
    user1.setPassword(request.getParameter("Password"));

    System.out.println(User);
}

protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    doGet(request, response);
       }
}
 User Java Class: public class User {

private String user;
private String password;

public User() {
}

public String getUser() {
    return user;
}

public void setUser(String user) {
    this.user = user;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
    }
}
 JSP Form ONLY:     form action="hello.html" method="GET"
doGet Name: <input type="text" name="name">
doGet Password: <input type="Password" name="Password" />
<input type="submit" value="Submit" />
    <input type="reset" value="Reset">
 XML FIle:     <display-name>HelloWorldWeb</display-name>
    <session-config>
    <session-timeout>30</session-timeout>
    </session-config>
    <welcome-file-list>
    <welcome-file>Hello.jsp</welcome-file>
    </welcome-file-list>
    <servlet>
    <description></description>
    <display-name>HelloServlet</display-name>
    <servlet-name>HelloServlet</servlet-name>
    <servlet-class>com.javavids.webapp.HelloWorldWeb.HelloServlet</servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>HelloServlet</servlet-name>
    <url-pattern>/hello.html</url-pattern>
    </servlet-mapping>
    </web-app>
answer(解决方案):

Here is the solution. HelloServlet: public class HelloServlet extends HttpServlet {
       private static final long serialVersionUID = 1L;

public HelloServlet() {
    super();
}

protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {

    String user = request.getParameter("name");
    String password = request.getParameter("Password");

    PACKAGENAME.User.setUser(user);
    PACKAGENAME.User.setPassword(password);
    request.setAttribute("log", user);
    request.setAttribute("pass", password);
    boolean result = PACKAGENAME.User.Auth(user, password);
    System.out.println(result);

    if(result == true) {
        RequestDispatcher rd = request.getRequestDispatcher("Welcome.jsp");
        rd.forward(request, response);
        System.out.println();
    } else {
        System.out.println();
        response.sendRedirect("Hello.jsp");
       }
}

protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    doGet(request, response);
    }
}
 User Java Class: public class User {

public static String User;
public static String Password;

public User() {
}

public static String getUser() {
    return User;
}

public static void setUser(String user) {
    User = user;
}

public static String getPassword() {
    return Password;
}

public static void setPassword(String password) {
    Password = password;
}

public static boolean Auth(String user, String password){

    if(Password.equals("123456") && User.equals("mansoor")){
        System.out.println("Password: " + Password);
        return true;  
    }
    else {
        return false;
      }  
   }
}
 Welcome.jsp: <body>
<h1>Welcome ${log}!</h1>
</body>
 Hello.jsp: <body>
<form action="HelloServlet" method="POST">
Name: <input type="text" name="name">
Password: <input type="Password" name="Password" />
<input type="submit" value="Submit" />
    <input type="reset" value="Reset">
</form>
</body>

 

 

 

 

 

 

 

异常中心网是一家专门收集整理程序员编程过程中遇到的常见异常(exception)以及各种异常问答中心的网站。异常中心网旨在,减少程序员在编码遇到异常,处理各种异常时间和痛苦,让程序员能更愉快的、快速的定位异常并查找对应的异常解决方案。异常中心网诚心打造最完美的编程社区为程序员用户服务,努力成为最好的程序员乐园程序员社区程序异常中心程序bug中心异常问答中心

 

喜欢0 评分0
游客

返回顶部