应用一、通过JSP打印“九九表”
效果如图:
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
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);
}
}
效果如图:
具体实现如下:
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
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中输出相应信息吧!