这是我的JSP代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<link rel="stylesheet" type="text/css" href="style/main.css">
<head>
<title>Trang chủ</title>
</head>
<body>
<h2>Hello World ! I am phucpdbk :D</h2>
<form style="width:400px; margin-left:auto;margin-right: auto;" method="POST" >
<fieldset>
<table cellpadding="10px" border="0">
<tr>
<td>Username:</td>
<td><input class="inputText" type="text" name="username" /></td>
</tr>
<tr>
<td>Password:</td>
<td><input class="inputText" type="password" name="password" /></td>
</tr>
<tr>
<td><input type="reset" value="Reset" /></td>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
</fieldset>
</form>
<%String message=(String) request.getAttribute("message"); %>
<p> <%=message %></p>
</body>
</html>
这是servlet:
package com.login;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
/**
* Servlet implementation class Login
*/
public class Login extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Login() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
*/
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Login Serverlet ! :D ");
String message="";
//String userName="username";
//String password="password";
String requestUser=request.getParameter("username");
String requestPass=request.getParameter("password");
Connection connect = null;
String url="jdbc:mysql://localhost:3306/";
String dbname="logindb";
String driver="com.mysql.jdbc.Driver";
String userNameMySql="root";
String passMySql="123123";
String stringQuery="select * from login where user='"+ requestUser+"' and password='"+requestPass+"'";
try {
Class.forName(driver).newInstance();
connect=DriverManager.getConnection(url+dbname,userNameMySql,passMySql);
Statement statement=connect.createStatement();
ResultSet resultSet = statement.executeQuery(stringQuery);
if(resultSet.next()){
message="Hello " + requestUser+".Login successful";
}
else{
message="Hello " + requestUser+".Login failed";
}
resultSet.close();
statement.close();
} catch (Exception e) {
e.printStackTrace();
}
request.setAttribute("message", message);
RequestDispatcher reqDispatcher = getServletConfig().getServletContext().getRequestDispatcher("/MavenWebApp/index.jsp");
reqDispatcher.forward(request,response);
}
}
web.xml
文件:
<!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>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>Login</servlet-name>
<display-name>Login</display-name>
<description>Login Serverlet</description>
<servlet-class>com.login.Login</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
这是控制台中的异常显示:
Login Serverlet ! :D
Nov 11, 2014 2:51:44 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [Login] in context with path [/MavenWebApp] threw exception
java.lang.NullPointerException
at com.login.Login.service(Login.java:79)
............................................
我已经在stackoverflow.com上进行过多次搜索,但它仍然无效。 对不起,如果我的问题让你不舒服,因为我是初学者,我的英语不好:D