应用一、通过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+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'Chart.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action ="ChartPrintServlet" method ="post"> <table border = "1px"> <tr><td>StartNumber</td><td><input type ="text" name = "start" id = "start"/></td></tr> <tr><td>EndNumber</td><td><input type ="text" name = "end" id = "end"/></td></tr> <tr><td><input type ="submit" value ="提交" id ="submit"/></td><td><input type ="reset" value ="重置" id ="reset"/></td></tr> </table> </form> </body> </html>
ChartPrint.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'ChartPrint.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <% 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+"<br>"); out.print(j+"*"+i+"="+j*i+" "); } out.print("<br>"); } %> </body> </html>
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+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'Chart2.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="ChartServlet" method="post"> <table border ="1px"> <tr><td>姓名:</td><td> <input type="text" name="name"/></td></tr> <tr><td>性别:</td><td> <input type="radio" value ="male" name ="sex"/>男 <input type="radio" value ="female" name ="sex"/>女</td></tr> <tr><td>地址:</td><td> <input type="text" name="address"/></td></tr> <tr><td>爱好:</td><td><input type="checkbox" name="hobby" value="sing"/>唱歌 <input type="checkbox" name="hobby" value="dance"/>跳舞<input type="checkbox" name="hobby" value="read"/>读书 <input type="checkbox" name="hobby" value="write"/>写字</td></tr> <tr><td border="0px"></td><td> <input type="submit" name="submit" value="提交"/> <input type="reset" name="reset" value="重置"/></td></tr> </table> </form> </body> </html>
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+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'Chart3.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <%=request.getAttribute("name") %><br/> <%=request.getAttribute("sex") %><br/> <%=request.getAttribute("address") %><br/> <%=request.getAttribute("hobby") %><br/> </body> </html>好了,赶快自己动在jsp中输出相应信息吧!