我是java的初学者。我正在尝试开发一个 Web 应用程序来验证 mysql 数据库中的用户名和密码。
当我运行程序时,我得到了异常。谁能帮我找出以下代码中的问题。
index.jsp
*********
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<form method="post" action="database">
<body>
<h1>Enter user details</h1>
<h2>Name</h2>
<input type="text" name="t1" /><br>
<h2>Password</h2>
<input type="password" name="p1" /><br><br>
<input type="submit" value="Submit" name="b1">
</body>
</form>
</html>
c1.java
********
package model;
public class c1 {
String name,password;
public c1(String name, String password) {
this.name = name;
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
database.java
*************
package controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
public class database extends HttpServlet {
@Resource(name = "db1")
private DataSource db1;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
String name=request.getParameter("t1").trim();
String password=request.getParameter("p1").trim();
int value=select(name,password);
if(value==1)
{
out.println("User details present in database");
}
else
{
out.println(value);
out.println("ERROR Invalid user");
}
}
catch(Exception e2)
{
out.println("error");
}
finally {
out.close();
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);`
}
@Override
public String getServletInfo() {
return "Short description";``
}// </editor-fold>
public int select(String UserName,String Password) throws SQLException
{
DataSource d= db1;
Connection c=d.getConnection();
PreparedStatement p=c.prepareStatement
("select * from Employee where name='"+UserName+"' and password='"+Password+"'");
ResultSet rs=p.executeQuery();
int a= rs.getRow();
return a;
}
}