美文网首页Servlet实例专题
在Servlet中向客户端写入Cookie信息

在Servlet中向客户端写入Cookie信息

作者: 神坛下的我 | 来源:发表于2018-08-19 19:36 被阅读0次

    UseServlet.java

    import java.io.IOException;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.*;
    
    
    @WebServlet("/user.do")
    public class UseServlet extends HttpServlet{
    
        private static final long serialVersionUID = 1L;
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            //设置字符集格式为utf-8
            req.setCharacterEncoding("UTF-8");
            String name= req.getParameter("name");
            //将用户名进行格式编码
            name=java.net.URLEncoder.encode(name, "utf-8");
            //将用户名保存到cookie对象中
            Cookie nameCookie = new Cookie("userName", name);
            //设置cookie过期之前的时间,单位为s
            nameCookie.setMaxAge(60);
                    nameCookie.setPath("/Servlet");
            //将cookie对象保存到客户端浏览器
            resp.addCookie(nameCookie);
            req.getRequestDispatcher("success.jsp").forward(req, resp);
            
        }
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            doPost(req, resp);
        }
    }
    

    index.jsp

    <form action="user.do" method="post">
        <%
            String userName=null;
            Cookie cookieArr[] = request.getCookies();
            if(cookieArr!=null&&cookieArr.length>0){
                for(Cookie c:cookieArr){
                    if(c.getName().equals("userName")){
                        userName = URLDecoder.decode(c.getValue(), "utf-8");
                                            userName.toString();
                    }
                }
            }
        %>
            <table align="center">
                <tr>
                    <td>用户名:</td>
                    <td><input type="text" name="name" value="<%if(userName!=null){out.println(userName);}%>"/></td>
                </tr>
                <tr>
                    <td>密码:</td>
                    <td><input type="password" name="pwd"/></td>
                </tr>
                <tr>
                    <td colspan="2" align="center">
                    <input type="submit" value="登录"/>
                    <input type="reset" value="重置"/>
                    </td>
                </tr>
            </table>
        </form>
    

    success.jsp

        <b>登录成功!</b><br>
        您的信息为:<br/>
        <table align="center">
                <tr>
                    <td>用户名:</td>
                    <td><%=request.getParameter("name") %></td>
                </tr>
                <tr>
                    <td>密码:</td>
                    <td><%=request.getParameter("pwd") %></td>
                </tr>
                <tr>
                <td><a href="index.jsp">【回到主页】</a></td></tr>
        </table>
    
    22.PNG 23.PNG 24.PNG

    相关文章

      网友评论

        本文标题:在Servlet中向客户端写入Cookie信息

        本文链接:https://www.haomeiwen.com/subject/exqhiftx.html