JavaWeb参考资料
out.println("</html>"); }} 从客户端读取 Cookie: public class GetCookieServlet extends HttpServlet{ public void doGet(HttpServletRequest request,HttpServletResponse response)
<td><input type="reset" value="重置" ></td> </tr> </table> </form> </body></html>
displayCustomer.jsp 示例(显示客户信息): <jsp:useBean id="customer" class="com.model.CustomerBean" scope="session">
//指定页面在传输过程中使用的编码方式 response.setHeader("Content-Encoding","gb2312"); response.setContentType("application/vnd.ms-excel;charset=gb2312"); PrintWriter out = response.getWriter(); out.println("学号\t 姓名\t 性别\t 年龄\t 所在系"); out.println("95001\t 李勇\t 男\t20\t 信息"); out.println("95002\t 刘晨\t 女\t19\t 数学"); }}
JavaWeb 参考资料(以书上为主)
1、Servlet 产生文档如 word、excel、图形等。P 48-49 public class ExcelServlet extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
inputCustomer.jsp 示例(输入客户信息): <%@ page contentType="text/html; charset=gb2312" %> <html><head> <title>Input a Customer</title></head> <body> <h4>Please Input a Customer</h4> <form action = "customer.do" method = "post">
this.custName = custName; this.email = email; this.phone = phone; }; // 访问方法 public String getCustName() { return this.custName; } public String getEmail() { return this.email; } public String getPhone() { return this.phone; } // 修改方法 public void setCustName(String custName) {
<jsp:setProperty name="customer" property="*"/> </jsp:useBean>
<html><body> <h4>The customer information is</h4> <table border="1"> <tr> <td>客户名:</td> <td><jsp:getProperty name="customer" property="custName"/></td> </tr> <tr> <td>Email 地址:</td> <td><jsp:getProperty name="customer" property="email"/></td> </tr> <tr> <td>电话:</td> <td><jsp:getProperty name="customer" property="phone"/></td> </tr> </table> </body></html>
this.custName = custName; } public void setEmail(String email) {
this.email = email; } public void setPhone(String phone) {
= phone; } }
CustomerServlet 示例(处理客户信息): public class CustomerServlet extends HttpServlet {
throws IOException,ServletException{ String cookieName = "username"; String cookieValue = null; Cookie[] cookies = request.getCookies(); if (cookies!=null){ for(int i = 0;i<cookies.length;i++){ Cookie cookie = cookies[i]; if(cookie.getName().equals(cookieName)) cookieValue = cookie.getValue(); }} response.setContentType("text/html"); PrintWriter out=response.getWriter(); out.println("<html><title>get cookies</title>"); out.println("<body><h2>A cookie has been got from browser</h2>"); out.println("CookieName:"+cookieName+"<br>"); out.println("CookieValue:"+cookieValue+"<br>"); out.println("</body></html>");
}}
4、JavaBean(模型)+Servlet(控制器)+Jsp(显示器),输入客户信息示例(无数据库)。 P 157 + P167-169 CustomerBean 示例(客户对象的模型): public class CustomerBean {
// 属性声明 private String custName; private String email; private String phone; public CustomerBean(){}; public CustomerBean(String custName,String email,String phone){
session.setAttribute("customer", customer); } RequestDispatcher view =
request.getRequestDispatcher("/displayCustomer.jsp"); view.forward(request,response); }}
<table> <tr><td>客户名:</td> <td><input type="text" name="custName" ></td></tr> <tr><td>邮件地址:</td><td><input type="text" name="email"></td></tr> <tr><td>电话:</td><td><input type="text" name="phone" ></td></tr> <tr><td><input type="submit" value="确定" ></td>
2、下载文档。P50 public class FileDownloadServlet extends HttpServlet{