-2

我在 STS 中创建了一个 Maven 项目。在项目中,我有 1 个 jsp 文件和 1 个 servlet。jsp 要求用户输入用户名和密码,然后 servlet 获取值,以 HTML 格式显示。但我也有 1 个 java 类。我希望将 jsp 文件中输入的数据存储在 java 类中定义的 2 个变量中,但我无法这样做。我尝试了几件事,但无法理解如何去做。

你好Servlet:

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);
       }
}

用户 Java 类:

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 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 文件:

    <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>
4

4 回答 4

0

这是解决方案。

你好Servlet:

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);
    }
}

用户 Java 类:

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; 
      }  
   }
}

欢迎.jsp:

<body>
<h1>Welcome ${log}!</h1>
</body>

你好.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>
于 2014-03-20T16:32:14.243 回答
0
user1.setUser(request.getParameter("name"));
    user1.setPassword(request.getParameter("Password"));

在此行之后尝试打印

System.out.println("Name "+user1.getUser()+"  password " +user1.getPassword)
于 2014-03-20T04:52:32.550 回答
0

您的 servlet 类应该在提交操作上被调用。你为什么做这个。

 form action="hello.html" method="GET"

将其更改为

 form action="/servletclass" method="POST"

从 Servlet 内部使用 RequestDispatcher 将控制权转发给 hello.html

于 2014-03-20T04:45:23.267 回答
0

将 method="get" 更改为 method="post" 并且:

String name = request.getParameter("name");
String passWord = request.getParameter("Password");
User user = new User();
user.setUser(name);
user.setPassword(passWord);
System.out.println(user.getUser + "----" +user.getPassword);
于 2014-03-20T04:53:05.807 回答