码迷,mamicode.com
首页 > 其他好文 > 详细

servlet之helloworld

时间:2014-12-29 22:55:32      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:

    最近要做个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

标签:

原文地址:http://www.cnblogs.com/malaya/p/4192465.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!