简体   繁体   English

HttpServletRequest获取请求

[英]HttpServletRequest getting request

I need really help. 我真的需要帮助。 I have a login.jsp page 我有一个login.jsp页面

<html>

<head></head>

<body>
  <form method="post" action="j_security_check">
    <table>
      <tr>
        <td>
          <input type="text" name="j_username" autofocus="autofocus" required="required" />
        </td>
      </tr>
      <tr>
        <td>
          <input type="password" name="j_password" required="required" />
        </td>
      </tr>
      <tr>
        <td>
          <input type="submit" value="Login" />
      </tr>
    </table>
  </form>
</body>

</html>

this is my web.xml 这是我的web.xml

All Pages /* 所有页面/ *
MY_User MY_用户

<security-role>
    <role-name>MY_User</role-name>
</security-role>

<login-config>
    <auth-method>FORM</auth-method>
     <realm-name>myRealm</realm-name>
    <form-login-config>
        <form-login-page>/login.jsp</form-login-page>
        <form-error-page>/error.jsp</form-error-page>
    </form-login-config>
</login-config>

In my filter I get the Principal. 在我的过滤器中,我得到了校长。 But somehow when I get into my main Class. 但是当我进入我的主要班级时。 The request is gone! 请求不见了!

How can I pass my Request to another Java class? 如何将我的请求传递给另一个Java类?

1) As per your query in comments, you can convert Command.java to a servlet just by extending it with the HttpServlet class. 1)根据注释中的查询,只需使用HttpServlet类进行扩展即可将Command.java转换为servlet。 Then you can have access to request and response objects in Command.java class. 然后,您可以访问Command.java类中的请求和响应对象。

2) other way to pass request and response objects to your Command.java class is as below: 2)将请求和响应对象传递到Command.java类的其他方法如下:
Let's say you have a servlet MyServlet from where you want to call the methods of Command.java class 假设您有一个要从其中调用Command.java类方法的Servlet MyServlet

public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

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

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try{
        Command c =new Command();//object creation depends on your project. e.g you can create it using beans of spring
        c.doSomething(request, response);
    }catch(Exception e){
        e.printStackTrace();
    }
}
}

Then, your Command class methods should have below parameters: 然后,您的Command类方法应具有以下参数:

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Command {

    public void doSomething(HttpServletRequest request,HttpServletResponse response) {
        System.out.println("doing something");
    }
}

Remember, Servlet request and response are just objects of a simple java class. 记住,Servlet请求和响应只是简单Java类的对象。 They does not depend on any api like servlet, you can pass these objects to any java class and their properties will remain same, no matter its servlet or a java class. 它们不依赖于servlet之类的任何api,您可以将这些对象传递给任何java类,并且无论其servlet还是java类,它们的属性都将保持不变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM