JavaWeb参考资料
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
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>");
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) {
2、下载文档。P50 public class FileDownloadServlet extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{ response.setContentType("application/jar"); // 设置响应头,将响应内容以指定的文件名存储到客户机上 response.setHeader("Content-Disposition","attachment;filename=servlet-api.jar"); File f = new File("C:\\servlet-api.jar"); FileInputStream is = new FileInputStream(f); ServletOutputStream os = response.getOutputStream(); byte[] bytearray = new byte[1024]; int bytesread = 0; while( (bytesread = is.read(bytearray) ) != -1 ){
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws java.io.IOException,ServletException { String name = request.getParameter("custName"); String email = request.getParБайду номын сангаасmeter("email"); String phone = request.getParameter("phone"); CustomerBean customer = new CustomerBean(name,email,phone); HttpSession session = request.getSession(); synchronized(session) {
JavaWeb 参考资料(以书上为主)
1、Servlet 产生文档如 word、excel、图形等。P 48-49 public class ExcelServlet extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
5、数据库操作(查找 select、新增 insert、删除 delete、更新 update)。P 208 SampleDAO.java 示例(包含增删改查语句): public class SampleDAO{
private static InitialContext context= null; private DataSource dataSource = null; private static final String SELECT_SQL = "SELECT * FROM customer"; private static final String GET_ONE_SQL =
}}
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); }}
this.custName = custName; } public void setEmail(String email) {
this.email = email; } public void setPhone(String phone) {
this.phone = phone; } }
CustomerServlet 示例(处理客户信息): public class CustomerServlet extends HttpServlet {
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">
os.write(bytearray, 0, bytesread); } os.flush(); }}
3、Cookie 的设置与读取。P 95-96 向客户 Cookie 对象: public class SendCookieServlet extends HttpServlet{
public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException{ Cookie userCookie = new Cookie("username", "hacker"); userCookie. setMaxAge(60*60*24*7); response.addCookie(userCookie); response.setContentType("text/html"); PrintWriter out=response.getWriter(); out.println("<html><title>add cookies</title>"); out.println("<body><h2>A cookie has been sent to browser</h2></body>");
<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>
//指定页面在传输过程中使用的编码方式 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 数学"); }}
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">
<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; 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) {
2、下载文档。P50 public class FileDownloadServlet extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{ response.setContentType("application/jar"); // 设置响应头,将响应内容以指定的文件名存储到客户机上 response.setHeader("Content-Disposition","attachment;filename=servlet-api.jar"); File f = new File("C:\\servlet-api.jar"); FileInputStream is = new FileInputStream(f); ServletOutputStream os = response.getOutputStream(); byte[] bytearray = new byte[1024]; int bytesread = 0; while( (bytesread = is.read(bytearray) ) != -1 ){
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws java.io.IOException,ServletException { String name = request.getParameter("custName"); String email = request.getParБайду номын сангаасmeter("email"); String phone = request.getParameter("phone"); CustomerBean customer = new CustomerBean(name,email,phone); HttpSession session = request.getSession(); synchronized(session) {
JavaWeb 参考资料(以书上为主)
1、Servlet 产生文档如 word、excel、图形等。P 48-49 public class ExcelServlet extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
5、数据库操作(查找 select、新增 insert、删除 delete、更新 update)。P 208 SampleDAO.java 示例(包含增删改查语句): public class SampleDAO{
private static InitialContext context= null; private DataSource dataSource = null; private static final String SELECT_SQL = "SELECT * FROM customer"; private static final String GET_ONE_SQL =
}}
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); }}
this.custName = custName; } public void setEmail(String email) {
this.email = email; } public void setPhone(String phone) {
this.phone = phone; } }
CustomerServlet 示例(处理客户信息): public class CustomerServlet extends HttpServlet {
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">
os.write(bytearray, 0, bytesread); } os.flush(); }}
3、Cookie 的设置与读取。P 95-96 向客户 Cookie 对象: public class SendCookieServlet extends HttpServlet{
public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException{ Cookie userCookie = new Cookie("username", "hacker"); userCookie. setMaxAge(60*60*24*7); response.addCookie(userCookie); response.setContentType("text/html"); PrintWriter out=response.getWriter(); out.println("<html><title>add cookies</title>"); out.println("<body><h2>A cookie has been sent to browser</h2></body>");
<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>
//指定页面在传输过程中使用的编码方式 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 数学"); }}
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">
<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>