赞
踩
在网络应用程序中有两种基本的结构,及C/S(客户机/服务器)和B/S(浏览器/服务器)。在当前的应用系统中,B/S占绝对主流地位。
在Web程序中,浏览器端与应用服务器端采用请求/响应模式进行交互,如下图所示:
该过程描述如下:
客户端(通常是浏览器,例如IE、Firefox等)接受用户的输入,例如用户名、密码、查询字符串等。
客户端像应用服务器发送请求,即输入之后提交,客户端把请求信息(包含表单中的输入以及其他请求等信息)发送到应用服务器端,客户端等待应用服务器端的响应。
数据处理,即应用服务器端使用某种脚本语言访问数据库、查询数据,并获得查询结果。
数据库像应用服务器中的程序返回结果。
发送响应,即应用服务器端向客户端发送响应信息(一般都是动态生成的HTML网页)。
显示,即用户的浏览器解析HTML代码,呈现用户界面。
HTTP是无状态的协议。Web页面本身无法向下一个页面传递信息,如果需要让下一个页面得知该页面中的值,除非通过服务器。
(1)
one.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <% String str = "123"; int num = Integer.parseInt(str); %> 该数字的平方和是:<%=num * num%> <a href="two.jsp?num=<%=num%>">two.jsp</a> </body> </html>
two.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <% String string= request.getParameter("num"); int num = Integer.parseInt(string); %> 传递过来的数字为:<%=num%> </body> </html>
(2)利用request.getRequestDispatcher(request,response)进行服务器内的跳转
(1)
one.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" import="java.util.*" %> <html> <head> <title>Title</title> </head> <body> <% String str = "12"; int num = Integer.parseInt(str); %> 该数字的平方和是:<%=num * num%><br> <a href="/Servlet?number=<%=num%>">Servlet</a> </body>
Servlet
public class Servlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/utf-8;charset=utf-8"); String str = request.getParameter("number"); int num = Integer.parseInt(str); PrintWriter out = response.getWriter(); out.print("我接受到的值是:" + num); }
运行结果:
(2)利用request.getRequestDispatcher(request,response)进行服务器内的跳转
one.jsp
String url = "/Servlet1?name=张三"; request.getRequestDispatcher(url).forward(request,response);
Servlet1
PrintWriter out = response.getWriter(); out.print("我接受的参数是:" + request.getParameter("name"));
运行截图同上图一样,在此就不再做过多赘述。。。。。。
Servlet1
public class Servlet1 extends HttpServlet { public Servlet1(){ super(); } public void init(){} protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); String string = request.getParameter("name"); PrintWriter out = response.getWriter(); out.print("我的名字是" + string); String url = "Cookie操作/one.jsp?name=张三"; request.getRequestDispatcher(url).forward(request,response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } public void destroy(){ super.destroy(); }
one.jsp
<% request.getParameter("name"); out.print("我接受到的姓名"+ request.getParameter("name")); %>
one.jsp
<form action="two.jsp" method="get"> 请输入你的姓名:<input type="text" name="name"><br> <input type="submit" value="提交"> </form>
two.jsp
<% request.setCharacterEncoding("utf-8"); String str = request.getParameter("name"); //int num = Integer.parseInt(str); out.print("我得到的参数是:" + str); %>
运行截图
one.jsp
<form action="/Servlet1" method="get"> 请输入你的姓名:<input type="text" name="name"><br> <input type="submit" value="提交"> </form>
Servlet
response.setContentType("text/html;charset=utf-8"); String string = request.getParameter("name"); PrintWriter out = response.getWriter(); out.print("我的名字是" + string);
运行截图
one.jsp
String str = "12"; int number = Integer.parseInt(str); %> 该数字的平方为:<%=number*number%> <% //讲str存入cookie Cookie cookie = new Cookie("number",str); //设置cookie的存活时间为600秒 cookie.setMaxAge(600); //讲cookie写入客户端 response.addCookie(cookie); %> <a href="two.jsp">到达two.jsp</a>
two.jsp
String str = null; Cookie[] cookies = request.getCookies(); for(int i = 0; i < cookies.length; i ++){ if (cookies[i].getName().equals("number")){ str = cookies[i].getValue(); break; } } out.print("我得到的参数是:" + str);
运行截图
one.jsp
String str = "12"; session.setAttribute("info",str); response.sendRedirect("two.jsp");
two.jsp
out.println("我接受到的参数是:" + session.getAttribute("info"));
one.jsp
<% String str = "12"; session.setAttribute("info",str); %> <a href="two.jsp">two
two.jsp
out.println("我接受到的参数是:" + session.getAttribute("info"));
one.jsp
<% String str = "12"; session.setAttribute("info",str); request.getRequestDispatcher("/Cookie操作/two.jsp").forward(request,response); %>
<% String str = "12"; session.setAttribute("info",str); %> <jsp:forward page="two.jsp"></jsp:forward>
one.jsp
String str = "12"; session.setAttribute("info",str) request.getRequestDispatcher("/Servlet").forward(request,response);
Servlet
String str = (String)request.getSession().getAttribute("info"); PrintWriter out = response.getWriter() out.print(str);
Servlet
request.getSession().setAttribute("info","12"); response.sendRedirect("Cookie/one.jsp");
one.jsp
String info = (String)session.getAttribute("info"); out.print(info);
Servlet
request.getSession().setAttribute("info","12"); request.getRequestDispatcher("/Cookie/one.jsp").forward(request,response);
one.jsp
String info = (String)session.getAttribute("info"); out.print(info);
Servlet
request.getSession().setAttribute("info","12"); request.getRequestDispatcher("/Servlet10").forward(request,response); 注意:这里的url地址中要和web.xml互相映射
Servlet1
out.print("我接收的参数是:" + request.getSession().getAttribute("info"));
one.jsp
<% request.setAttribute("info","你好"); String str = "/Cookie操作/two.jsp"; request.getRequestDispatcher(str).forward(request,response); %>
one.jsp
<% out.println("我接受到的参数是:" + request.getAttribute("info")); %>
one.jsp
<% request.setAttribute("info","你好"); request.getRequestDispatcher(str).forward(request,response); %> <jsp:forward page="two.jsp"></jsp:forward>
two.jsp
<% out.println("我接受到的参数是:" + request.getAttribute("info")); %>
运行截图
注意:同样该传值也可以用request.getRequestDispatcher(url).forward(request,response)进行跳转
servlet
request.setAttribute("info",str); request.getRequestDispatcher("/Servlet1").forward(request,response);
servlet1
out.print("我接收的参数是:" + request.getAttribute("info"));
运行截图
注意:这里request.getRequestDispatcher(url).forward(request,response)中的url地址要和web.xml中配置的地址要保持一致!!!!!!
同样的,在servlet向jsp传值的时候有两种方法,第一种是response.sendRedirect()方法,第二种是request.getRequestDispatcher()方法,如果传值的话就需要用request.getRequestDispatcher().forward(request,response)方法
Servlet
String str = "12"; request.setAttribute("info",str); request.getRequestDispatcher("/Cookie操作/one.jsp").forward(request,response);
one.jsp
out.print("我接收到的值是:" + request.getAttribute("info"));
运行截图
Servlet
String str = "12"; request.setAttribute("info",str); request.getRequestDispatcher("/Servlet10").forward(request,response);
Servlet1
out.print("我接收的参数是:" + request.getAttribute("info"));
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。