创建Servlet Cookie [英] Creating Servlet Cookies

查看:93
本文介绍了创建Servlet Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对servlets比较陌生,目前我正在努力添加cookie。我相信这是一个模块化方法的结果,我采取,我建立一个头部实用程序类,只是将所有的头信息插入到servlet中,所以我想采取相同的方法与添加cookie。

I am rather new to servlets and currently I'm struggling with adding cookies. I believe this is a result of a modular approach i am taking in that I built a header utility class that simply inserts all the header information into the servlet so I want to take the same approach with adding cookies.

我还有一个凭证验证器类,除了用户名和密码,验证它,然后返回一个有效/无效的响应。这里我相信谎言的问题。在将用户名和密码传递给凭证验证程序的登录表单中,我将表单操作定向到凭证servlet,将表单方法作为帖子。

I also have a crediential validator class that excepts the user name and password, validates it, then returns a valid/invalid response. Here i believe lies the problem. In the login form that passes the username and password to the credential validator, I have the form action directing to the credential servlet and the form method as a post.

如果我想将一个值从表单发送到另一个servlet,这样做会有问题吗?

Doing it this way provides a problem if i want to send a value from the form to another servlet, or does it?

这个项目的目标是为学校创建一个简单的网站,严格地使用servlet,然后我们使用JSP来缓解疼痛。

The goal of this project, for school, is to create a simple website strictly with servlets then we get to use JSP to ease the pain.

我应该考虑另一种方法吗?当使用表单操作和方法时,是否可以让这些类在表单上执行各种函数?

Is there another approach i should be considering? Is it possible to have these classes that perform a variety of functions on forms when the form action and method are utilized?

感谢您的任何帮助和指导。

Thank you for any help and guidance.

最佳
E

推荐答案

如果需要,然后将请求转发到另一个servlet。

You could send requests to a servlet and then forward requests to another servlet if needed.

在您的情况下,验证后,您可以将结果存储在一个属性中,然后将控制转移到另一个servlet。 (如果这是你想要做的)

In your case, after validation, you can store result in an attribute and then transfer control to another servlet. (if that's what you want to do)

 RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/urlToServlet");
 dispatcher.forward(request, response);

这是如何处理Cookie。

And this is how to deal with cookies.

创建和发送Cookie

Cookie userCookie = new Cookie("name", "value");
userCookie.setMaxAge(60*60*24*365); //Store cookie for 1 year
response.addCookie(userCookie);

从客户端读取Cookie

String cookieName = "somecookie";
Cookie[] cookies = request.getCookies();
if (cookies != null) 
{
    for(int i=0; i<cookies.length; i++) 
    {
        Cookie cookie = cookies[i];
        if (cookieName.equals(cookie.getName())) 
        {
            doSomethingWith(cookie.getValue());
        }
    }
}
else
{
    //do something else for firsttime visitors 
}   

您是否使用Cookie进行会话跟踪?
如果是,则使用 HttpSession

Are you using cookies for session tracking? If yes, then use HttpSession. Using HttpSession then there is not need to directly involve with cookies for session tracking.

例如,在一个简单的登录页面,这是你做的

For example, in a simple login page, this is what you do

HttpSession session = request.getSession();
session.setAttribute("username",username);
In other pages,
if(session.getAttribute("username")==null)
{
//forward to login page.
}

这篇关于创建Servlet Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆