1.建立servlet,在其中输入如下代码:
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloWorldServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
//浏览器获得的代码
response.setContentType("text/html;charset=gb2312");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>HelloWorld</title>");
out.println("</head>");
out.println("<body bgcolor=\"white\">");
out.println("<hr>");
out.println("HelloWorld");
out.println("你好!世界");
out.println("</body></html>");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
//发送到浏览器
doGet(request, response);
}
}
2.在web.xml中输入如下代码:
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>HelloWorldServlet</servlet-name>
<servlet-class>servlet.HelloWorldServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorldServlet</servlet-name>
<url-pattern>/servlet/HelloWorldServlet</url-pattern>
</servlet-mapping>
在浏览器里输入http://localhost:8080/servletExample1/servlet/HelloWorldServlet,查看servlet程序。
其中URL地址访问servlet是参考<url-pattern>标签里的内容。
posted on 2007-09-13 09:20
虎胆闯天下 阅读(1848)
评论(2) 编辑 收藏 所属分类:
Java