Servlet和JSP的分工

来源:互联网 发布:mac os用xmind超慢 编辑:程序博客网 时间:2024/04/28 02:24

jsp和Servlet的分工:
  * JSP:
    > 作为请求发起页面,例如显示表单、超链接。
    > 作为请求结束页面,例如显示数据。
  * Servlet:
    > 作为请求中处理数据的环节。

来看一张图:




下边显示一个小Demo,在一个jsp页面中输入两个参数,在另一个页面中将两者相加的结果显示。

AServlet.Java

[java] view plain copy
  1. package com.ywq;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.PrintWriter;  
  5.   
  6. import javax.servlet.RequestDispatcher;  
  7. import javax.servlet.ServletException;  
  8. import javax.servlet.http.HttpServlet;  
  9. import javax.servlet.http.HttpServletRequest;  
  10. import javax.servlet.http.HttpServletResponse;  
  11.   
  12. public class AServlet extends HttpServlet {  
  13.   
  14.   
  15.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  16.             throws ServletException, IOException {  
  17.           
  18.         //从form.jsp页面获取参数  
  19.         String num1=request.getParameter("num1");  
  20.         String num2=request.getParameter("num2");  
  21.           
  22.         //参数类型转换  
  23.         int a=Integer.parseInt(num1);  
  24.         int b=Integer.parseInt(num2);  
  25.           
  26.         int sum=a+b;  
  27.           
  28.         //将运算结果保存在request域中  
  29.         request.setAttribute("result", sum);  
  30.           
  31.         //请求转发,使转换到显示结果页面。  
  32.         RequestDispatcher rd=request.getRequestDispatcher("/add/result.jsp");  
  33.         rd.forward(request, response);  
  34.     }  
  35.   
  36. }  

form.jsp

[plain] view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>这个页面用来输入两个参数</title>  
  13.       
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.     <meta http-equiv="description" content="This is my page">  
  19.     <!--  
  20.     <link rel="stylesheet" type="text/css" href="styles.css">  
  21.     -->  
  22.   
  23.   </head>  
  24.     
  25.   <body>  
  26.    <form action="/day11_1/AServlet" method="post">  
  27.     加数1:<input type="text" name="num1"/><br>  
  28.     加数2:<input type="text" name="num2"/><br>  
  29.     <input type="submit" value="运算">  
  30.    </form>  
  31.   </body>  
  32. </html>  


result.jsp

[plain] view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>运算结果显示页面</title>  
  13.       
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.     <meta http-equiv="description" content="This is my page">  
  19.     <!--  
  20.     <link rel="stylesheet" type="text/css" href="styles.css">  
  21.     -->  
  22.   
  23.   </head>  
  24.     
  25.   <body>  
  26.    <%  
  27.         Integer sum=(Integer)request.getAttribute("result");  
  28.     %>  
  29.       
  30.     <%=sum %>  
  31.   </body>  
  32. </html>  

将Project部署到Tomcat中,启动服务器,在浏览器中输入http://localhost:8080/day11_1/add/form.jsp,则出现下图所示:


输入两个参数,点击按钮,则出现如下所示:


项目工程截图如下:





原创粉丝点击