我在下面开发了一个 servlet,其中逻辑是如果名称等于 saral saxena,那么它会移动到下一页,否则不会,我也在我的代码下面发布,我想要一个更改是它也应该是访客编号这样,如果来自不同浏览器的两个不同用户点击相同的 url,那么对于第二个,它应该显示您是 2 号访客。下面是我目前的一段代码..
package trial;
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;
/**
* Servlet implementation class SendRedirectServlet
*/
public class SendRedirectServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse
response)throws ServletException, IOException {
//System.out.println("*************");
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
String name = request.getParameter("username");
pw.println("<br> this output is generated by a simple servlet.");
String password = request.getParameter("password");
if(name.equals("saral")&& password.equals("saxena")){
response.sendRedirect("/ValidUserServlet");
}
else{
pw.println("u r not a valid user");
}
}
}
另一个servlet是...
ackage trial;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ValidUserServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter pw = response.getWriter();
pw.println("Welcome to roseindia.net " + " ");
pw.println("how are you");
}
}
我现在的 web.xml 仪式是......
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<servlet>
<servlet-name>s1</servlet-name>
<servlet-class>trial.SendRedirectServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>s1</servlet-name>
<url-pattern>/SendRedirectServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>s2</servlet-name>
<servlet-class>trial.ValidUserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>s2</servlet-name>
<url-pattern>/ValidUserServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>home.html</welcome-file>
</welcome-file-list>
</web-app>
最后我的html页面是......
<html>
<head>
<title>New Page 1</title>
</head>
<body>
<form method="post" action="/SendRedirectServlet">
<p>Enter your name
<input type="text" name="username" size="20"/></p>
<p>Enter your password <input type="text" name="password"
size="20"/></p>
<p>
<input type="submit" value="Submit" name="B1"/></p>
</form>
</body>
</html>
项目结构的快照是..
http://imageshack.us/photo/my-images/707/49690689.jpg/
请告知如何添加您是访客号的功能。
伙计们请指教。