Http协议无状态带来的问题和解决方法
2016-08-15 00:46
218 查看
HTTP是无状态协议,也就是没有记忆力,每个请求之间无法共享数据。
会话跟踪技术:
1.参数传递机制
是在请求路径后面加上请求的参数
localhost/dowork/login?username=xxx
问题:将请求参数暴露在地址栏中,不安全,使用后面的cookie和session解决
2.cookie
3.session
<form action="/dowork/login" method="post">
<input placeholder="用户名" type="text" name="username" /> <br/>
<input placeholder="密码" type="password" name="password" /> <br/>
<input type="submit" value="登录" />
</form> 值传递最少要页面三个才能看出效果 如果是两个页面的话直接请求就可获得值:
在下面我演示的都是简单的动态页面 我教的是理念 而不是代码
将要演示的代码结构:
//javaWeb结构很重要 如有不懂 等我有空 在抽时间写张关于javaWeb结构的文章
@WebServlet("/login")
public class LoginServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//设置请求格式
req.setCharacterEncoding("utf-8");
//设置响应格式
resp.setContentType("text/html;charset=utf-8");
PrintWriter out = resp.getWriter();
//获取请求时的用户名和密码
String username = req.getParameter("username");
String password = req.getParameter("password");
//验证用户名和密码
if("admin".equals(username)&&"1234".equals(password)){
//进入邮箱界面
out.println("<h1>欢迎登录:"+username+"的邮箱</h1>");
out.println("<a href='/dowork/list?username="+username+"'>"+"未读邮件</a>");
}else{
System.out.println("登录失败");
}
out.close();
}
}
@WebServlet("/list")
public class ListServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//设置请求的格式
req.setCharacterEncoding("utf-8");
//设置响应格式
resp.setContentType("text/html;charset=utf-8");
String username = req.getParameter("username");
PrintWriter out = resp.getWriter();
out.println("欢迎查看:"+username+"邮件");
out.println("<a href = '/dowork/content?username="+username+"'>邮件1</a>");
out.println("<a href = '/dowork/content?username="+username+"'>邮件2</a>");
out.println("<a href = '/dowork/content?username="+username+"'>邮件3</a>");
out.close();
}
}
解决方法就放在下一张博客吧 请期待Cookie和Session
会话跟踪技术:
1.参数传递机制
是在请求路径后面加上请求的参数
localhost/dowork/login?username=xxx
问题:将请求参数暴露在地址栏中,不安全,使用后面的cookie和session解决
2.cookie
3.session
<form action="/dowork/login" method="post">
<input placeholder="用户名" type="text" name="username" /> <br/>
<input placeholder="密码" type="password" name="password" /> <br/>
<input type="submit" value="登录" />
</form> 值传递最少要页面三个才能看出效果 如果是两个页面的话直接请求就可获得值:
在下面我演示的都是简单的动态页面 我教的是理念 而不是代码
将要演示的代码结构:
//javaWeb结构很重要 如有不懂 等我有空 在抽时间写张关于javaWeb结构的文章
@WebServlet("/login")
public class LoginServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//设置请求格式
req.setCharacterEncoding("utf-8");
//设置响应格式
resp.setContentType("text/html;charset=utf-8");
PrintWriter out = resp.getWriter();
//获取请求时的用户名和密码
String username = req.getParameter("username");
String password = req.getParameter("password");
//验证用户名和密码
if("admin".equals(username)&&"1234".equals(password)){
//进入邮箱界面
out.println("<h1>欢迎登录:"+username+"的邮箱</h1>");
out.println("<a href='/dowork/list?username="+username+"'>"+"未读邮件</a>");
}else{
System.out.println("登录失败");
}
out.close();
}
}
@WebServlet("/list")
public class ListServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//设置请求的格式
req.setCharacterEncoding("utf-8");
//设置响应格式
resp.setContentType("text/html;charset=utf-8");
String username = req.getParameter("username");
PrintWriter out = resp.getWriter();
out.println("欢迎查看:"+username+"邮件");
out.println("<a href = '/dowork/content?username="+username+"'>邮件1</a>");
out.println("<a href = '/dowork/content?username="+username+"'>邮件2</a>");
out.println("<a href = '/dowork/content?username="+username+"'>邮件3</a>");
out.close();
}
}
@WebServlet("/content") public class ContentServlet extends HttpServlet{ private static final long serialVersionUID = 1L; protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String username = req.getParameter("username"); resp.setContentType("text/html;charset=utf-8"); PrintWriter out = resp.getWriter(); out.print("最后一页啦:"+username+"大大"); out.print("<br/>宝宝炸了"); out.close(); } }
解决方法就放在下一张博客吧 请期待Cookie和Session
相关文章推荐
- Windows下安装Apache HTTP Server 无法启动遇到OS10048 通常每个套接字地址(协议/网络地址/端口)只允许使用一次 的问题解决方法
- php 解决文件状态缓存带来的问题
- WEB上调用HttpWebRequest奇怪问题的解决方法
- SQL 2005 Report Server 请求因 HTTP 状态 400 失败: Bad Request 处理方法 的解决方法
- VS2005不能新建智能设备项目的解决方法(非IE8带来的问题)
- WCF 部署问题 小总结 (HTTP 不能注册的解决方法 和 部署Bat脚本)
- ASP.NET 未被授权访问所带来的问题的解决方法。
- 解决HTTP数据不更新问题——no-cache在HTTP通信协议中的应用
- 异步 HttpContext.Current实现取值的方法(解决异步Application,Session,Cache...等失效的问题)
- 电脑不能连接远程桌面方法(彻底解决因操作系统带来的问题)
- WF 自定义活动开发常见问题(进入第二个流程状态,数据传递失败的解决方法)
- 安装完vs.2005之后,重新安装iis后无法使用http方式访问asp.net工程的页面的问题的解决方法
- XMLHttpRequest乱码问题在Firefox与IE下的不同解决方法
- HttpCompress相关问题解决方法
- 采用二级域名带来的问题及解决方法
- Http 1.1 500错误问题解决方法
- 解决内网通过代理服务器访问外网部署WebService报错问题“请求因 HTTP 状态 407 失败”
- ASP.NET State service状态服务的问题解决方法
- 异步 HttpContext.Current实现取值的方法(解决异步Application,Session,Cache...等失效的问题)
- 异步 HttpContext.Current实现取值的方法(解决异步Application,Session,Cache...等失效的问题)