我的cactus学习之旅

环境配置:jdk1.6,eclipse 3.4.2,jetty插件(http://run-jetty-run.googlecode.com/svn/trunk/updatesite/)

‍注:jetty(http://jettylauncher.sourceforge.net/updates)不能使用,可能是和eclipse3.4.2不兼容吧。

我的学习过程

1.创建一个Dynamic Web Project,名称为“cactusTest”;设置java build path为“cactusTest/WebContent/WEB-INF/classes”。

‍2.创建index.jsp,welcome.jsp,LoginServlet.java

index.jsp

*******************************************************************************************************************************

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<html>

<head>

  <TITLE>首页</TITLE>

</head>

<body>

  <form action="/testCactus/LoginServlet">

   登入名称: <input type="text" name="username"><br>

   登入密码:<input type="password" name="password"><br>

   <input type="submit" value="登入">

  </form>

</body>

</html>

----------------------------------------------------------------------------------------------------------------------------------

welcome.jsp

*******************************************************************************************************************************

<%@page language="java" contentType="text/html" pageEncoding="UTF-8" %>

<html>

<head>

<TITLE>欢迎页</TITLE>

</head>

<body>Hello, <%= request.getAttribute("username") %>!

</body>

</html>

----------------------------------------------------------------------------------------------------------------------------------

LoginServlet.java

*******************************************************************************************************************************

package com.test.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;

import javax.servlet.ServletException;

import javax.servlet.http.*;

public class LoginServlet extends HttpServlet {

   public void doGet(HttpServletRequest req, HttpServletResponse resp)

   throws ServletException, IOException {

   doPost(req, resp);

   }

   public void doPost(HttpServletRequest req, HttpServletResponse resp)

   throws ServletException, IOException {

   if (isValidUser(req)) {

   String username = req.getParameter("username");

   req.setAttribute("username", username);

   RequestDispatcher rd = req.getRequestDispatcher("/welcome.jsp");

   rd.forward(req, resp);

   }

   else {

   //do something

   }

   }

   public boolean isValidUser(HttpServletRequest req) {

   String username = req.getParameter("username");

   String password = req.getParameter("password");

   if (username.equals("eryuanz") && password.equals("1234")) {

   return true;

   }

   else {

   return false;

   }

   }

}

----------------------------------------------------------------------------------------------------------------------------------

3.从cactus官网下载最新版cactus-1.8.1-bin(http://jakarta.apache.org/site/downloads/downloads_cactus.cgi),解压到本地(cactus-1.8.1-bin)。

4.将cactus-1.8.1-bin/lib下servlet-api-2.4.jar,junit-3.8.2.jar,commons-logging-1.1.jar,commons-httpclient-3.1.jar,cactus.core.framework.uberjar.javaEE.14-1.8.1.jar,aspectjrt-1.5.3.jar 放到工程cactusTest/WebContent/WEB-INF/lib下。

5.编辑WEB-INF/web.xml,内容如下:<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

"http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

<display-name>My Web Application</display-name>

<description>

A application for test.

</description>

<servlet>

<servlet-name>ServletRedirector</servlet-name>

<servlet-class>org.apache.cactus.server.ServletTestRedirector</servlet-class>

</servlet>

<servlet>

<servlet-name>JspRedirector</servlet-name>

<jsp-file>/jspRedirector.jsp</jsp-file>

</servlet>

<servlet>

<servlet-name>ServletTestRunner</servlet-name>

<servlet-class>org.apache.cactus.server.runner.ServletTestRunner</servlet-class>

<init-param>

  <param-name>xsl-stylesheet</param-name>

  <param-value>/cactus-report.xsl</param-value>

</init-param>

</servlet>

<servlet>

<servlet-name>LoginServlet</servlet-name>

<servlet-class>com.test.servlet.LoginServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>ServletRedirector</servlet-name>

<url-pattern>/ServletRedirector</url-pattern>

</servlet-mapping>

<servlet-mapping>

<servlet-name>JspRedirector</servlet-name>

<url-pattern>/JspRedirector</url-pattern>

</servlet-mapping>

<servlet-mapping>

<servlet-name>ServletTestRunner</servlet-name>

<url-pattern>/ServletTestRunner</url-pattern>

</servlet-mapping>

<servlet-mapping>

<servlet-name>LoginServlet</servlet-name>

<url-pattern>/testCactus/LoginServlet</url-pattern>

</servlet-mapping>

</web-app>

----------------------------------------------------------------------------------------------------------------------------------

6.创建LoginServletTest.java(放在与src同级的test下)

package com.test.servlet;

import org.apache.cactus.ServletTestCase;

import org.apache.cactus.WebRequest;

public class LoginServletTest extends ServletTestCase {

public void beginValidUser(WebRequest webreq) {

  webreq.addParameter("username", "eryuanz");

  webreq.addParameter("password", "1234");

}

public void testValidUser() {

  LoginServlet login = new LoginServlet();

  assertTrue(login.isValidUser(request));

}

}

----------------------------------------------------------------------------------------------------------------------------------

7.在eclipse中配置jetty,并启动jetty.

 

 

余下的在 http://hi.baidu.com/z%C5%B6%C5%B6/blog,等我有空再贴。。。。

 

你可能感兴趣的:(apache,eclipse,jsp,Web,servlet)