(这是随书附赠的光盘)HTML+XHTML+CSS+JavaScript+DOM+AJAX语法及范例实用辞典附赠光盘

  • h3_846836
    了解作者
  • 746.6KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-06-15 13:25
    上传日期
《HTML+XHTML+CSS+JavaScript+DOM+AJAX语法及范例实用辞典》这本书的附赠光盘,内容有从chapter1-chapter5的所有网站源码。
HTML+XHTML等实例.rar
内容介绍
import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class ProxyServlet extends HttpServlet { private String contentUrl; public void init(ServletConfig servletConfig) throws ServletException { contentUrl = "http://myhost/employees.xml"; } public void service (HttpServletRequest httpRequest, HttpServletResponse httpResponse)throws ServletException{ URL content = null; try { content = new URL(contentUrl); } catch (MalformedURLException exception) { throw new ServletException(contentUrl+"地址有误,请检查!"); } URLConnection contentCon = null; try { contentCon = content.openConnection(); } catch (IOException exception) { throw new ServletException( "打开URL地址"+contentUrl+"时出错:"+ exception.toString()); } // 从URLConnection获取内容类型并设置响应 String contentType = contentCon.getContentType(); httpResponse.setContentType(contentType); // 读取输入流 try { InputStream in = contentCon.getInputStream(); byte[] buffer = new byte[1024]; ByteArrayOutputStream contentStream = new ByteArrayOutputStream(); for(int cnt=in.read(buffer);cnt!=-1;cnt=in.read(buffer)){ contentStream.write(buffer, 0, cnt); if (in.available() == 0) { break; } } // 将内容转换成字符串 String contentString = contentStream.toString(); System.err.println(contentString); // 发送响应 byte[] contentBytes = contentString.getBytes(); OutputStream out = httpResponse.getOutputStream(); out.write(contentBytes,0,contentBytes.length); out.flush(); out.close(); } catch (Exception exception) { throw new ServletException("不能代理该请求:"+exception); } } }
评论
    相关推荐