使用Resin3.0配置运行Java Servlet

我是布衣中布衣

我是布衣中布衣

2016-02-19 13:37

今天图老师小编要向大家分享个使用Resin3.0配置运行Java Servlet教程,过程简单易学,相信聪明的你一定能轻松get!
前提:正确安装JDK和Resin3.0
  这个是原始文档,我做一下注释
  Configuring the web.XML
  The following is a complete working web.xml to run this example.
  The servlet-mapping tells Resin that the URL /hello should invoke the hello-world servlet.
  The servlet tells Resin that hello-world uses the test.HelloWorld class and that the value of the greeting init parameter is Hello World.
  
  WEB-INF/web.xml
  web-app>
  servlet-mapping url-pattern='/hello'
  servlet-name='hello-world'/>
  
  servlet servlet-name='hello-world'
  servlet-class='test.HelloWorld'>
  init-param greeting='Hello, World'/>
  /web-app>
  
  WEB-INF/web.xml //web.xml不一定有自己新建一个即可,这个是我改过的!
  web-app>
  servlet servlet-name='hello-world'
  servlet-class='test.HelloWorld'/> servlet-mapping url-pattern='/hello'
  servlet-name='hello-world'/>
  /web-app>
  
  原始文档中有三个错误,我也不知道是不是我错了!但是我改过三个错误,运行成功!
  1。servlet servlet-name标签应该放在servlet-mapping前面。
  2。servlet servlet-name='hello-world' servlet-class='test.HelloWorld'>标签结尾少了一个" / "。
  3。init-param greeting='Hello, World'/> 这句标签我去掉了,不然也无法运行!
  The Java code, HelloWorld.java belongs in
  
  $app-dir/WEB-INF/classes/test/HelloWorld.java
  
  Or, if you're compiling the servlet yourself, the class file belongs in
  
  $app-dir/WEB-INF/classes/test/HelloWorld.class
  
  Following is the actual servlet code. It just prints a trivial Html page filled with the greeting specified in the web.xml.
  
  init() and destroy() are included mostly for illustration. Resin will call init() when it starts the servlet and destroy before Resin destroys it.
  
  package test;
  
  import java.io.*;
  import javax.servlet.*;
  import javax.servlet.http.*;
  
  public class HelloWorld extends HttpServlet {
  private String greeting;
  
  public void init()
  throws ServletException
  {
  greeting = getInitParameter("greeting");
  }
  
  public void doGet(HttpServletRequest request,
  HttpServletResponse response)
  throws ServletException, IOException
  {
  PrintWriter out = response.getWriter();
  
  out.println("title>" + greeting + "/title>");
  out.println("h1>" + greeting + "/h1>");
  }
  
  public void destroy()
  {
  // nothing to do
  }
  }
  
  它文档中的这个用了那个init-param greeting='Hello, World'/>标签,由于我删除了,所以无法使用!
  就用着吧!保存为HelloWorld.java放在WEB-INF/classes/test目录下面。
  
  package test;
  import java.io.*;
  import java.util.*;
  import javax.servlet.*;
  import javax.servlet.http.*;
  
  public class HelloWorld extends HttpServlet{
  public void doGet(HttpServletRequest req, HttpServletResponse res)
  throws ServletException, IOException
  {
  res.setContentType("text/html");
  PrintWriter pw = res.getWriter();
  pw.println("!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">");
  pw.println("head>");
  pw.println("meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">");
  pw.println("!-- The Servlet eXPression tags interpolate script variables into the HTML --");
  pw.println("title>Hello, world!/title>");
  pw.println("/head>");
  pw.println("body bgcolor=#cc99dd>");
  pw.println("h1>Hello, world!/h1>");
  pw.println("/body>");
  pw.close();
  }
  }
  
  开启Resin服务,在浏览器中输入 http://localhost:8080/hello
  即可以正确访问!
展开更多 50%)
分享

猜你喜欢

使用Resin3.0配置运行Java Servlet

编程语言 网络编程
使用Resin3.0配置运行Java Servlet

Java环境 使用Resin在NT环境下配置JSP环境

编程语言 网络编程
Java环境 使用Resin在NT环境下配置JSP环境

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

Java Servlet及Cookie的使用

Java JAVA基础
Java Servlet及Cookie的使用

使用Java Servlet动态生成图片

编程语言 网络编程
使用Java Servlet动态生成图片

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

如何配置Java Server Page运行环境

编程语言 网络编程
如何配置Java Server Page运行环境

使用servlet和Java Bean访问MySQL

编程语言 网络编程
使用servlet和Java Bean访问MySQL

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

用NetBeans平台开发J2ME游戏实例讲解3

用NetBeans平台开发J2ME游戏实例讲解3

CSS自动换行

CSS自动换行
下拉加载更多内容 ↓