首先,要保证能正常访问http://youserver/appdir/servlet/testservlet.
我将你的文件稍微改动了一下,因为我不明白index.jsp是干啥的
流程是 先访问q1.jsp 什么也不填 ,提交后,servlet转向到login.jsp
或直接在浏览器的URL输入"http://myserver/mydir/servlet/indexServlet?cmd"
也可转向到login.jsp
我的环境为Linux 8.0+tomcat 4.1.18+ j2sdk1.4
q1.jsp
<html>
<body>
<form method=post action="servlet/indexServlet" >
Please Input your name:
<input type=text name="cmd">
<input type=submit value="submit">
</form>
</body>
</html>
login.jsp
<style type="text/css">
</style>
<form name="form" method="post" action="dologin.jsp" target="_top">
<center>
<table width="32%" border="0">
<tr>
<td align="right" width="21%">username:</td>
<td>
<input type="text" name="username" size="20" class="input">
</td>
</tr>
<tr>
<td align="right" width="21%">password:</td>
<td>
<input type="password" name="password" size="20" class="input">
</td>
</tr>
<tr>
<td width="21%">
<input type="submit" name="Submit" value="Submit">
</td>
<td>
<input type="reset" name="Submit2" value="Reset">
</td>
</tr>
</table>
</center>
</form>
indexServlet.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class indexServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html;
charset=GB2312";
//Initialize global variables
public void service(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException
{
String cmd=req.getParameter("cmd");
//String[] cmd=req.getParameterValues("cmd");
// PrintWriter out = resp.getWriter();
// out.println(cmd);
if(cmd==null)
{
resp.sendRedirect("../login.jsp");
}
if (cmd.equals("login"))
{
resp.sendRedirect("../login.jsp");
}
if (cmd.equals("logout"))
{
resp.sendRedirect("../logout.jsp");
}
resp.sendRedirect("../login.jsp");
}
}