J2EE 第2次课笔记--Servlet

类别:Java 点击:0 评论:0 推荐:

这次课讲了一些servlet一些基础的知识!

1.在本地访问servlet(以tomcat服务器为例):

     http://locatlhost:8080/servlet/你的servlet的名字

    这样你就可以直接访问这个servlet了!



2.使用jcreator进行servlet的程序编写时,需要导入tomcat下,common/lib/servlet.jar

   才能调用servlet包


3.测试程序,还是从最简单的Hello,World!来进行练习吧!

   代码:

      import javax.servlet.*;
      import java.io.*;
      public class HelloServlet extends GenericServlet
      {
       //需要重载初象类service
       public void service(ServletRequest request,ServletResponse response) throws IOExcetpion
        {
            PrintWriter out = response.getWriter();
            out.println("Hello,this is our world!");
         }
      }



4.HttpServlet继承了GenericServlet,HttpServletRequest/HttpServletResponse继承ServletRequest/ServletResponse

   方法getHeader是得到表头信息

            request.getHeader(“connection“)  得到连接信息

           request.getHeader(“User-Agent“) 得到用户信息
   
  例子:

      import javax.servlet.*;
      import java.io.*;
      import javax.servlet.http.*;    //必须导入这个包,才能使用HttpServlet

      public class HelloServlet extends HttpServlet
      {
       //需要重载初象类service
       public void service(HttpServletRequest request,HttpServletResponse response) throws IOExcetpion
        {
            PrintWriter out = response.getWriter();
            String s = request.getHeader("Connection");
             String user = request.getHeader("User-Agent");
            out.println("you connect to server is"+s);
            out.close();
     }
  }


5.response.setContentType("text/html;charset=gb2312")

本文地址:http://com.8s8s.com/it/it11346.htm