我从LoginPage.jsp调用LoginCheckServlet,但是当我转发页面时,我不想让URL成为LoginCheck。如果登录正确,那么我想使用/ Home转发到HomePage.jsp,如果登录不正确,我想转发到LoginRetry.jsp并使用/ LoginRetry作为url模式。
这是LoginCheckServlet Servlet。如您所见,我有@WebServlet注释(" / LoginCheck")因此显示的网址为:http://localhost:8080/BankWebApp/LoginCheck
package org.innominds.intern.BankWebApp.Servlets;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.context.support.AbstractApplicationContext;
import org.innominds.intern.BankWebApp.Database.*;
import org.innominds.intern.BankWebApp.Original.*;
/**
* Servlet implementation class LoginCheckServlet
*/
@WebServlet("/LoginCheck")
public class LoginCheckServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LoginCheckServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//String contextPath = request.getContextPath();
RequestDispatcher rd = null;
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
UserJdbcTemplate jdbc = (UserJdbcTemplate)context.getBean("UserJdbcTemplate");
String username = request.getParameter("uname");
System.out.println(username);
String password = request.getParameter("password");
User currentUser = jdbc.getUser(username);
if(currentUser != null && currentUser.checkPassword(password)){
request.setAttribute("currentUser", currentUser);
//HomeServlet hs = new HomeServlet();
//hs.doPost(request, response);
rd = request.getRequestDispatcher("/Home"); //Calls HomeServlet which forwards to HomePage.jsp, URL should be http://localhost:8080/BankWebApp/Home
}
else{
//LoginRetryServlet lr = new LoginRetryServlet();
//lr.doPost(request, response);
rd = request.getRequestDispatcher("/LoginRetry"); //Calls LoginRetryServlet which forwards to LoginRetry.jsp, URL should be http://localhost:8080/BankWebApp/LoginRetry
}
rd.forward(request,response);
}
}
JDBC编码没有问题,并确定用户名和密码是否正确,但我只是想更改URL模式,使其看起来很稳固。
这是我从中调用LoginCheckServlet的doPost方法的LoginPage.jsp。
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login Page</title>
</head>
<body>
<h1 align = "center"> Login </h1>
<br/>
<br/>
<form action="LoginCheck" method = "post">
<table border = "1" align = "center">
<tr><td align = center width = 200>Username</td><td><input type="text" name= "uname"/></td></tr>
<tr><td align = "center" width = 200>Password</td><td><input type="password" name= "password"/></td></tr>
<!-- <tr><td></td><td align = "center"><input type="submit" value = "Login"/></td></tr> -->
</table>
<br/>
<center><input align = "center" type="submit" value = "Login"/></center>
</form>
</body>
</html>
我还没有在web.xml中完成任何映射,它基本上是空的。每次我尝试像我下面注释的那样进行映射时,我得到:localhost上的服务器Tomcat v8.0服务器无法启动。
<?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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>BankWebApp</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>WelcomePage.jsp</welcome-file>
</welcome-file-list>
<!-- <servlet>
<description></description>
<servlet-name>HomeServlet</servlet-name>
<servlet-class>org.innominds.intern.BankWebApp.Servlets</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HomeServlet</servlet-name>
<url-pattern>/Home</url-pattern>
</servlet-mapping> -->
</web-app>
有关其他信息,我使用Maven和Spring依赖项。这可能没有必要,但这是我的pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>BankWebApp</groupId>
<artifactId>BankWebApp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>commons-digester</groupId>
<artifactId>commons-digester</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
</project>