我正在尝试使用表单获取用户名 n 密码。我正在使用存储在 mysql 表中的用户名 n 密码对其进行验证。但它没有被执行......有人可以告诉我有什么问题吗???Thnx 快速回复.. ;-)
*package mypack;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class SaveServlet
*/
public class SaveServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public SaveServlet() {
// 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
String u = request.getParameter("username");
String p=request.getParameter("password");
System.out.println(u);
System.out.println(p);
String c="jdbc:mysql://localhost:3306/test";
Connection con=null;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(c, "root", "MyNewPass");
PreparedStatement pst=con.prepareStatement("select * from userinfo where username="+u+" and password="+p+";");
System.out.println("inside resultset");
ResultSet rs=pst.executeQuery();
System.out.println("inside resultset");
while(rs.next())
{
System.out.println("inside resultset");
}
}
catch (Exception e) {
// TODO: handle exception
System.out.println("Failed");
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}*
html
<%@ 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>
<script type="text/javascript">
function validate(){
var x = document.forms["form1"]["username"].value;
if (x == null || x == "") {
alert("Fill the User Id to Login");
return false;
}
var y = document.forms["form1"]["password"].value;
if (y == null || y == "") {
alert("Password Please");
return false;
}
else{
document.form1.submit();
return true;
}
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<center>
<form action="SaveServlet" name="form1">username <input
type="text" name="username" /> <br>
<br>
password <input type="password" name="password"></input><br>
<br>
<input type="button" value="login" onclick="validate()"> </form>
</center>
</body>
</html>