gpt4 book ai didi

用于检查数据库中是否存在用户名和密码的java Web应用程序

转载 作者:行者123 更新时间:2023-11-29 13:46:56 26 4
gpt4 key购买 nike

我是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;

}
}

最佳答案

您的代码中有几个错误:

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>
<body>
<form method="post" action="database">>
<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">
</form>
</body>
</html>

JAVA。您的 Controller 中有一些错误。一些 ` 字符会产生错误。

正确代码:

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
processRequest(request, response); // ERROR Removed last `
}

@Override
public String getServletInfo() {
return "Short description"; // ERROR removed lasts ``
}// </editor-fold>

WEB.XML:检查您的 web.xml 并验证是否有以下几行:

<servlet>
<servlet-name>Database-servlet</servlet-name>
<servlet-class>controller.database</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Database-servlet</servlet-name>
<url-pattern>/database</url-pattern>
</servlet-mapping>

关于用于检查数据库中是否存在用户名和密码的java Web应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17270586/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com