我正在做一个uni项目,试图将用户信息添加到我的数据库中,或者检查信息是否在数据库中,以便用户可以登录,但是当我单击“提交”按钮时,将java文件添加到我的数据库中我用来连接数据库的代码所在的便携式计算机,而不是执行代码。
这是我的html表单:
这是我的java类login.java-单击html网页上的Submit按钮时,将下载此文件,而不是执行代码:
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class auth extends HttpServlet {
private static final long serialVersionUID = 1L;
public auth() {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws
ServletException, IOException {
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws
ServletException, IOException {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
String id = request.getParameter("id");
String password = request.getParameter("password");
String sql = "select * from reg where id='" + id + "'";
Connection conn = null;
try {
conn =
DriverManager.getConnection("jdbc:mysql://localhost/gan",
"root", "password");
Statement s = conn.createStatement();
java.sql.ResultSet rs = s.executeQuery(sql);
String un = null;
String pw = null;
String name = null;
/* Need to put some condition in case the above
query does not return any row, else code will throw Null Pointer
exception */
PrintWriter prwr1 = response.getWriter();
if(!rs.isBeforeFirst()){
prwr1.write("
No Such User in
Database
");
} else {
/* Conditions to be executed after at least one
row is returned by query execution */
while (rs.next()) {
un = rs.getString("id");
pw = rs.getString("password");
}
PrintWriter pww = response.getWriter();
if (un.equalsIgnoreCase(username) &&
pw.equals(pass)) {
// use this or create request dispatcher
response.setContentType("text/html");
pww.write("
Welcome, " + name + "
");
} else {
pww.write("wrong username or password\n");
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
我希望它连接到我的mysql数据库,但是它只是下载了login.java文件,我之前从未见过这种情况,因此,任何帮助都将非常有用,谢谢!