javaWeb学习之旅(三)-----java WEB的开发入门
三、java WEB的开发
1.服务器的配置服务器的详细安装步骤可以看我的博客:
/**** * * 数据的压缩 * **/private void test2(HttpServletResponse response) throws IOException {String data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";System.out.println("原始数据的大小:" + data.length());ByteArrayOutputStream bout = new ByteArrayOutputStream();GZIPOutputStream gout = new GZIPOutputStream(bout);gout.write(data.getBytes());gout.close();// 得到压缩后的数据byte gzip[] = bout.toByteArray();System.out.println("压缩后的大小" + gzip.length);// 通知浏览器数据采用的压缩格式response.setHeader("Content-Encoding", "gzip");response.setHeader("Content-Length", gzip.length + "");response.getOutputStream().write(gzip);}3.content-type --- 控制以那个方式处理数据
/** * 通过content-type的头字段,控制以那个方式处理数据 * ***/private void Test3(HttpServletResponse response) throws IOException {response.setHeader("content-type", "image/jpeg");InputStream in = this.getServletContext().getResourceAsStream("/2.jpg");int len = 0;byte buffer[] = new byte[1024];OutputStream outputStream = response.getOutputStream();while ((len = in.read(buffer)) > 0) {outputStream.write(buffer, 0, len);}}
/*** * refresh头字段的用法,每隔3秒访问以下百度 * **/private void test4(HttpServletResponse response) throws IOException {response.setHeader("refresh", "3;url='http://www.baidu.com'");String data="111111111111";response.getOutputStream().write(data.getBytes());}5.content-dispostion头---实现下载功能
//实现下载功能private void test5(HttpServletResponse response) throws IOException {response.setHeader("content-dispostion", "attachment;filename=3.jpg");InputStream in = this.getServletContext().getResourceAsStream("/3.jpg");int len = 0;byte buffer[] = new byte[1024];OutputStream outputStream = response.getOutputStream();while ((len = in.read(buffer)) > 0) {outputStream.write(buffer, 0, len);}}
// 实现断点续传public static void main(String[] args) throws Exception {URL url = new URL("http://localhost/web01/a.txt");HttpURLConnection connection = (HttpURLConnection) url.openConnection();connection.setRequestProperty("Range", "bytes=5-");InputStream inputStream = connection.getInputStream();int len = 0;byte buffer[] = new byte[1024];FileOutputStream outputStream = new FileOutputStream("d:\\a.txt", true);while ((len = inputStream.read(buffer)) > 0) {outputStream.write(buffer, 0, len);}inputStream.close();outputStream.close();}
public class ServletDemo1 extends HttpServlet implements SingleThreadModel {int i = 0;//线程安全问题:用synchronized 解决public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {i++;try {Thread.sleep(1000 * 3);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}response.getOutputStream().write((i + " ").getBytes());}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}}
public class SevletConfig extends HttpServlet {/***** * ServletConfig 对象:用于封装servlet的配置信息 * 在实际开发中,有一些东西不是适合在servlet程序中写死,这类数据可以通过配置方式配置给Servlet 比如: servlet 采用哪个码表 * servlet 链接哪个库, servlet 哪个配置文件 * * ********/public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {// 得到指定的配置String value = this.getServletConfig().getInitParameter("data");System.out.println(value);// 得到所有的配置信息Enumeration enumeration = this.getServletConfig().getInitParameterNames();while (enumeration.hasMoreElements()) {String name = (String) enumeration.nextElement();String valueall = this.getServletConfig().getInitParameter(name);System.out.println(name + "==" + valueall);}}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {}}
<servlet><servlet-name>SevletConfig</servlet-name><servlet-class>com.nyist.servlet.SevletConfig</servlet-class><init-param><param-name>data</param-name><param-value>dddddd</param-value></init-param><init-param><param-name>data1</param-name><param-value>hhhhh</param-value></init-param><init-param><param-name>config</param-name><param-value>/struts-config.xml</param-value></init-param><init-param><param-name>charset</param-name><param-value>UTF-8</param-value></init-param><init-param><param-name>url</param-name><param-value>jdbc:mysql://localhost:80/test</param-value></init-param><init-param><param-name>username</param-name><param-value>root</param-value></init-param><init-param><param-name>password</param-name><param-value>root</param-value></init-param></servlet>
//1、得到Servletcontext的方法ServletContext context=this.getServletConfig().getServletContext();//2context=this.getServletContext();
String putdata="传递数据"; this.getServletContext().setAttribute("datastr", putdata);共享数据2
String value=(String) this.getServletContext().getAttribute("datastr");System.out.println(value);2、获取web应用的初始化参数
String value=this.getServletContext().getInitParameter("data");response.getOutputStream().write(("<font color='red'>")+value+"</font>").getBytes();
//下面是通过ServletContext 实现请求转发的功能String data="ddddddddddddddd";//把数据带个1.jsp (不能通过context 域 需要通过request域)this.getServletContext().setAttribute("data", data);RequestDispatcher requestDispatcher=this.getServletContext().getRequestDispatcher("/1.jsp");requestDispatcher.forward(request, response);
body>This is my JSP page.<br><h1><font color="red"> <% String data = (String) application.getAttribute("data"); out.write(data); %> </font></h1></body>4.利用ServletContext对象读取资源文件
/**** * 读取资源文件 其中db.properties的路径 如果有包名则换成文件名 如com.nyist.servlet * 应该为com/nyist/servlet/db.properties 如果放在WEB-INF下直接就可以使用 db.properties * * * **/InputStream inputStream = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");/** * 或者还有一个方法可以替换上一句代码 调用getServletContext 获取资源的绝对路径, * 再通过传统的流读取资源文件,有点是可以获取当前读取资源文件的名称 * * String path = this.getServletContext().getRealPath( * "WEB-INF/classes/db.properties"); // \ 转义字符获取最后一个斜杠后面的文件名称\ String * filename=path.substring(path.lastIndexOf("\\")+1); * System.out.println("当前资源的名称是:"+filename); FileInputStream * inputStream1 = new FileInputStream(path); * **/Properties properties = new Properties();properties.load(inputStream);String url = properties.getProperty("url");String usrname = properties.getProperty("usrname");String password = properties.getProperty("password");System.out.println(url);System.out.println(usrname);System.out.println(password);db.properties的文件如下
url=jdbc:mysql://localhost:80/testusrname=rootpassword=root
// 使用Servlet调用其它程序ServletContext context = this.getServletContext();UsrDao dao = new UsrDao();dao.update();
public class UsrDao {public void update() throws Exception {String path = UsrDao.class.getClassLoader().getResource("db.properties").getPath();FileInputStream inputStream = new FileInputStream(path);Properties properties = new Properties();properties.load(inputStream);System.out.println(properties.getProperty("url"));}}