深入体验JavaWeb开发内幕——JSP技术相关案例应用

应用一、通过JSP打印“九九表”

创建一个Web Project工程。
编写.jsp页面,页面中提供两个输入文本框,用来收集用户提交的数据,并提供“提交”和“取消”按钮,方便用户提交或取消相应的操作。
l

效果如图:

jsp代码

Chart.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'Chart.jsp' starting page
    
	
	
	    
	
	
	

  
  
  
  
StartNumber
EndNumber

ChartPrint.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'ChartPrint.jsp' starting page
    
	
	
	    
	
	
	

  
  
  
    <%
      String startNumber = (String)request.getAttribute("start");
      String endNumber = (String)request.getAttribute("end");
      int st =new Integer(startNumber);
      int se =new Integer(endNumber);
      for( int i=1;i<=se;i++){
          for(int j=st;j<=i;j++){
            // out.print(j+"*"+i+"="+j*i+"
"); out.print(j+"*"+i+"="+j*i+" "); } out.print("
"); } %>


ChartPrintServlet.java

package net.csdn.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ChartPrintServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String start= request.getParameter("start");
		String end= request.getParameter("end");
		request.setAttribute("start", start);
		request.setAttribute("end", end);
		request.getRequestDispatcher("ChartPrint.jsp").forward(request, response);

	
		}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}

二、使用JSP相关知识通过将jsp页面中的表单信息在服务端获取然后在另一个jsp页面进行输出。

效果如图:

具体实现如下:

Chart2.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'Chart2.jsp' starting page
    
	
	
	    
	
	
	

  
  
  
  
姓名:  
性别:    男  
地址:  
爱好:唱歌 跳舞读书 写字
        

ChartServlet .java

package net.csdn.servlet;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class ChartServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {request.setCharacterEncoding("utf-8");response.setContentType("text/html;charset=utf-8");PrintWriter out = response.getWriter();String name = request.getParameter("name");System.out.println(name);String sex =request.getParameter("sex").trim();String address =request.getParameter("address").trim();String hobby =request.getParameter("hobby").trim();if((name.equals("")&&name==null)){System.out.println("用户名不能为空!");request.getRequestDispatcher("Chart2.jsp").forward(request, response);return ;}else{request.setAttribute("name", name);request.setAttribute("sex", sex);request.setAttribute("address", address);request.setAttribute("hobby",hobby);request.getRequestDispatcher("Chart3.jsp").forward(request, response);}}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}}
 
  

Chart3.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'Chart3.jsp' starting page
    
	
	
	    
	
	
	

  
  
  
      <%=request.getAttribute("name") %>
<%=request.getAttribute("sex") %>
<%=request.getAttribute("address") %>
<%=request.getAttribute("hobby") %>
好了,赶快自己动在jsp中输出相应信息吧!

你可能感兴趣的:(深入体验JavaWeb开发内幕——JSP技术相关案例应用)