好久不写东东了,小结一下

来源:互联网 发布:修罗武神听书全集软件 编辑:程序博客网 时间:2024/04/29 14:06

话说上次在csdn看见一个女生的空间,顿时决定不再QQ空间写那些技术东西了,要写就拿到这里来写,烂就烂总归有这么多高手在这里指点,人家不也是一点一滴过来的吗?现在,从她写的东西能看出她的技术很牛了。

这次我想好好理理jsp的那几个内置对象,从servlet的角度。先来段代码:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld extends HttpServlet

public HelloWorld() {
  super();
 }
 public void destroy() {
  super.destroy(); 
 }


public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
{

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><head><title>");
out.println("my first Servlet");
out.println("</title></head><body>");
out.println("<h1>Hello,World!</h1>");
out.println("</body></html>");

}

public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><head><title>");
out.println("my first Servlet");
out.println("</title></head><body>");
out.println("<h1>Hello,World!</h1>");
out.println("</body></html>");

}
public void init(ServletConfig config) throws ServletException {
  super.init(config);
 }


}

一上是一个简单的servlet其中已展示的对象有request, response, config以及out等,从中我们可以知道request为HttpServletRequest实例,response为HttpServletResponse实例,out为PrintWriter实例且可以由response得getWriter()获得,out和在浏览器上打印的字符串有关。config是ServletConfig对象,和配置文件有关,如可以获得web.xml中的初始化参数(config的getInitParameter("valueName")方法)。

这里自然少不了session和application,session为HttpSession对象,可以有request的getSession()方法获得,他有setAttribute(String name, Object object)和getAttribute(String name)方法存储和获得对象,有唯一ID关键字;application与ServletContext接口,可以由config的getServletContext()获得。

暑假一直在研究jsp,加油啊!