注:如需转载,请附上原文链接,如有建议或意见,欢迎批评指正!

需求说明:

// index.jsp页面
1 <%
2 String basePath = request.getScheme()+":"+"//"+request.getServerName()+":"+request.getServerPort()+"/"
3 +request.getServletContext().getContextPath()+"/";
4 %>
5 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
6 <html>
7 <head>
8 <title>ServletDemo加法运算</title>
9 </head>
10 <body>
11 <%--action: 表示访问的servlet路径--%>
12 <%out.print("basePath意味着:" + basePath);%>
13 <form action="<%=basePath%>ServletDemo1" method="post">
14 a: <input type="text" name="a"><br>
15 b: <input type="text" name="b"><br>
16 <input type="submit" value="计算"/><br>
17 </form>
18 </body>
19 </html>
// ServletDemo1.java
 import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException; @WebServlet("/ServletDemo1")
public class ServletDemo1 extends HttpServlet {
@Override
public void init() throws ServletException {
System.out.println("init()方法");
} protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("doPost()方法");
doGet(request, response);
} protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String a = request.getParameter("a");
String b = request.getParameter("b");
int sum = Integer.valueOf(a) + Integer.valueOf(b);
request.setAttribute("sum", sum);
// 方式一:PrintWriter对象写入
// response.getWriter().print(sum);
// 方式二:请求转发
// request.getRequestDispatcher("sum.jsp").forward(request, response);
// 方式三:重定向
ServletContext sc = request.getServletContext();
sc.setAttribute("sum2", sum);
response.sendRedirect("sum2.jsp");
System.out.println("doGet()方法");
} @Override
public void destroy() {
System.out.println("destroy()方法");
}
} // sum.jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>请求跳转求和</title>
</head>
<body>
a + b = <%=request.getAttribute("sum")%>
</body>
</html> // sum2.jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>重定向跳转求和</title>
</head>
<body>
a + b = <%=application.getAttribute("sum2")%>
</body>
</html>

index.jsp页面效果图展示:

1. 方式一:PrintWriter对象写入效果图:

2. 方式二:请求转发效果图:

3. 重定向效果图:

最新文章

  1. frp内网穿透配置
  2. Folder Recursion with C#
  3. [原创]可动态显示圆形图像或圆形文字的AvatarImageView
  4. centos6.5 ssh安全优化,修改默认端口名,禁止root远程登录
  5. ArcSDE for oracle10g安装后post的时候出现错误
  6. xcode设置 - App内存暴增
  7. Pie(二分)
  8. oracle 优化——索引与组合索引
  9. c++多线程编程之互斥对象(锁)的使用之----死锁
  10. 【Oracle】RAC添加新节点
  11. 汇编语言学习——第二章 寄存器(CPU工作原理)
  12. WisDom .net开发框架设计 2
  13. css 修改input中placeholder提示问题颜色
  14. Client将数据读写HDFS流程
  15. bzoj3545: [ONTAK2010]Peaks 重构树 主席树
  16. java 解压zip java.lang.IllegalArgumentException: MALFORMED 错误
  17. vue基于d2-admin的RBAC权限管理解决方案
  18. java如何写入txt文件
  19. Post方式调用wcf服务
  20. Java高级架构师(一)第40节:更多模块的基本功能和配置

热门文章

  1. Mock测试,何去何从
  2. vs2015的密钥
  3. 2015-09-15-git配置
  4. 如何单独卸载office2016中的one note
  5. Win32 计时器
  6. LVS(注意iptables和selinux的问题)
  7. maven工程根项目运行ok但是子项目就报错的解决办法
  8. H5本地离线存储
  9. Oil Deposits(油田)(DFS)
  10. 3D打印如何重组制造格局?