首页 > 代码库 > servlet之helloworld

servlet之helloworld

    最近要做个html转pdf的测试,但开发环境中代码太过庞大,于是新建了个Web项目来做测试。但又不想配置spring之类的框架,于是写了个Servlet,

 

java:

public class Hello extends HttpServlet {        /**        *        */        private static final long serialVersionUID = 1L;        public void init(ServletConfig config) throws ServletException       {               super.init(config );       }        public void service(HttpServletRequest reqest, HttpServletResponse response)                      throws IOException { // 获得一个向客户发送数据的输出流:              PrintWriter out = response.getWriter();               response.setContentType( "text/html;charset=GB2312");// 设置响应的 MIME 类型。               out.println( "<HTML> <BODY>");               out.println( "Simple servlet");               out.println( "</body> </html>");       }}

web.xml中还要配置,不然就用不了了:

        <servlet>         <servlet-name> Hello</ servlet-name>         <servlet-class> servlet.Hello</servlet-class >        </servlet>               <servlet-mapping>         <servlet-name> Hello</ servlet-name>         <url-pattern> /Hello</ url-pattern>        </servlet-mapping>

最后再新建个index.jsp:

index.jsp:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ><%@ page    language = "java" contentType = "text/html; charset=GB18030"   pageEncoding ="GB18030"  %><HTML>  <HEAD>  <TITLE> servlet test </TITLE>  </HEAD>  <BODY>  <form action= "Hello" id =‘myform‘>servlet test<input type= ‘button‘ value =‘test‘ onclick=‘submit()‘/></form></BODY>  <script type= "text/javascript"> function submit(){        myform.submit();        }</script></HTML>

一切OK。

写多了spring,这种纯servlet还是难得写。。

servlet之helloworld