嗨,
我目前正在玩Guice和@SessionScoped.为了更有意义,我决定构建一个(非常简单的)身份验证过程.
下面,我将解释我所做的每一步.然后我会问你一些问题.
[1] 我创建了一个代表一个人(访客或用户)的Identity类:
@SessionScoped
public class Identity implements Serializable
{
private String uid;
private String name;
public boolean isAuthenticate()
{
return uid != null;
}
public void logout()
{
this.uid = null;
}
/*Setters-Getters*/
}
Run Code Online (Sandbox Code Playgroud)
[2] 接下来,我创建了一个登录用户的Authentication类:
public class Authentication
{
@Override
public Identity authenticate(String login, String password)
{
/*some code*/
Identity identity = new Identity();
identity.setUid(user.getId());
return identity;
}
}
Run Code Online (Sandbox Code Playgroud)
[3] 然后,在我的Servlet中,我登录用户:
@RequestScoped
public class LoginAction
{
@Inject
Injector injector;
protected void login(HttpServletRequest req, HttpServletResponse resp)
{ …Run Code Online (Sandbox Code Playgroud) 上周我发现了Guice ...我正在尝试一些简单的技巧.但是,我目前被封锁......
我正在尝试将请求转发到由包含" * " 的url-pattern服务的Servlet中的JSP .但我一直收到"错误404":(
一步步 :
ServletModule :
serve("/test/*").with(TestServlet.class);
Run Code Online (Sandbox Code Playgroud)
TestServlet :
public void doGet(HttpServletRequest req, HttpServletResponse resp)
{
System.err.println("Start");
try
{
req.getRequestDispatcher("/WEB-INF/layout/test.jsp").forward(req, resp);
}
catch (Exception e)
{
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
HTTP错误404
访问/WEB-INF/layout/test.jsp时出现问题.原因:/
WEB-INF/layout/test.jsp
我测试了"serve("/ test").with(TestServlet.class);" 它工作
我没有Guice测试(通过在web.xml中定义servlet),它工作...
感谢阅读!