全部博文(83)
分类: Java
2005-12-15 16:18:54
1.先在TOMCAT下建立一个测试的工程TestServlet,然后从别的工程拷贝过来一个WEB-INF,其中最少有web.xml和classes!
2.先修改一下web.xml,基本格式是:
3.在工程下写一个测试的jsp页面,名为TestJsp.jsp:
<%@ page contentType="text/html;charset=gb2312" %>
<%@ page import="java.util.*,java.lang.*"%>
<%
int num = 0;
Integer strTmp = (Integer)request.getAttribute("ReturnNum");
if (strTmp!=null)
{
num=strTmp.intValue();
}
%>
4.在eclipse下建立一个Servlet工程,导入servlet.jar包
建立com.servlet.TestingServlet,与web.xml里的servlet相对应
package com.servlet;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.*;
public class TestingServlet extends HttpServlet
{
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
doPost(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
doExcute(request, response);
}
private void doExcute(HttpServletRequest request,
HttpServletResponse response)
{
RequestDispatcher rd = null;
/* 获取上下文环境 */
ServletContext sc = null;
String strAction = null;
strAction = request.getParameter("numC");
int i=Integer.valueOf(strAction).intValue();
i=i+1;
String strSuccessPageURL = "/TestJsp.jsp";//这是查询主页面
System.out.println("调用了业务逻辑查询 " + strAction);
request.setAttribute("ReturnNum",new Integer(i));
try
{
/* 获取上下文环境 */
sc = getServletContext();
/* 设置返回地址 */
rd = sc.getRequestDispatcher(strSuccessPageURL);
/* forward到结果页面 */
rd.forward(request, response);
} catch (ServletException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
5.把eclipse中刚才建立的TestingServlet的class文件导入tomcat的TestServlet工程的WEB-INF下的classes下,
6.重启tomcat后,敲入就可以看到效果了!