建立基本的Java Web站点

       1.创建站点及虚拟目录。在Eclipse中创建新的Dynamic Web Project(这里命名为WebTest)。在文件WebContent目录下新建index.html ,然后在Tomcat中创建指向WebContent的虚拟路径(这里命名虚拟路径为jtest)。配置完成后在浏览器中输入地址http://127.0.0.1:8080/jtest/index.html,测试站点是否启动运行。

      2.测试jsp容器。在WebContent目录下新建index.jsp文件,在文件的body中输入"Success! <%=new java.util.Date()%> ",在浏览器中访问该文件,查看jsp文件是否被正常解析.

      3.创建Servlet。在src源码目录下创建com.test.servlet.TestServlet类。内容如下:

   1:  package com.test.servlet;
   2:   
   3:  import java.io.IOException;
   4:  import java.io.PrintWriter;
   5:   
   6:  import javax.servlet.ServletException;
   7:  import javax.servlet.http.HttpServlet;
   8:  import javax.servlet.http.HttpServletRequest;
   9:  import javax.servlet.http.HttpServletResponse;
  10:   
  11:  @SuppressWarnings("serial")
  12:  public class TestServlet  extends HttpServlet {
  13:      protected void service(HttpServletRequest request, HttpServletResponse response) 
  14:      throws ServletException, IOException { 
  15:          PrintWriter out=response.getWriter(); 
  16:          out.println("

Test success!

"
);
  17:          out.flush(); 
  18:      } 
  19:  }

    上面的HttpServlet类在Servlet-api.jar中,需要添加jar引用。然后编译该类,并输出到WEB-INF/classes目录下。

4.配置Servlet。在web.xml中的web-app小节中添加以下Servlet配置:

   1:  <servlet>
   2:          <servlet-name>TestServlet
    servlet-name>
   3:          <servlet-class>com.test.servlet.TestServlet
    servlet-class>
   4:  
    servlet>
   5:  <servlet-mapping>
   6:        <servlet-name>TestServlet
    servlet-name>
   7:        <url-pattern>/test/*
    url-pattern>
   8:    
    servlet-mapping>

以上配置声明了一个名称为TestServlet的Servlet,该Servlet由com.test.servlet.TestServlet实现,然后把该Servlet映射到url的/test/路径下,这样在浏览器中通过地址http://127.0.0.1:8080/jtest/test/xxx就可以访问到该Servlet,页面输出:Test success!

 

      5.使用bean。bean是一个普通的class,在src源码目录下创建类com.test.bean.MyBean,内容如下:

   1:  package com.test.bean;
   2:   
   3:  public class MyBean {
   4:      private String name = ""; 
   5:      public MyBean(String strName_p){ //构造
   6:          this.name=strName_p; 
   7:      }  
   8:      public void setName(String strName_p){ //set属性
   9:          this.name=strName_p; 
  10:      } 
  11:      public String getName(){ //get属性
  12:          return this.name; 
  13:      }  
  14:  }

然后在jsp文件中应用该bean,jsp内容如下:

   1:  
    xml version="1.0" encoding="UTF-8" ?>
   2:  <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
   3:  <%@ page import="com.test.bean.MyBean" %>  
   4:  
    DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   5:  <html xmlns="http://www.w3.org/1999/xhtml">
   6:  <head>
   7:  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
   8:  <title>Insert title here
    title>
   9:  
    head>
  10:  <body>
  11:  <center> 
  12:  <% 
  13:      MyBean testBean=new MyBean("This is a test java bean."); 
  14:  %> 
  15:  Java bean name is: <%=testBean.getName()%> 
  16:  
    center>  
  17:  
    body

在浏览器中通过地址http://127.0.0.1:8080/jtest/test.jsp访问该文件,输出“Java bean name is: This is a test java bean.”

 

至此,java Web站点的基本配置演示就完成了。

你可能感兴趣的:(建立基本的Java Web站点)