我对此有点怀疑...我如何从 servlet 中名为“z”的 jsp 页面 jscript 字符串中设置/检索值。我需要在 servlet 中使用它...我正在探索新事物 n它对我来说是新事物,因为我对这些东西很陌生...感谢您的快速帮助....如果 pass1 和 pass2 相同,我需要密码的值,然后如果 pass1 我需要在 servlet 中检索它==pass2...告诉我一个方法...为此我写了一个 jscript 来检查 pass1==pass2..
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>New User Registration</title>
<script>
function myFunction(){
var x = document.forms["newForm"]["pass1"].value;
var y = document.forms["newForm"]["pass2"].value;
if(x==y){
document.newForm.submit();var z=x;
return true;
}
else {
alert("Passwords not matching!!!");}
}
</script>
</head>
<body>
<h1>Form</h1>
<fieldset>
<form name=newForm action="RegServlet">Username:<input
type="text" name="username"><br>
Password:<input type="text" name="pass1" id="pass1"><br>
Confirm Password:<input type="text" name="pass2" id="pass2"><br>
<input type="submit" onclick=myFunction() value="Create"></input></form>
</fieldset>
</body>
</html>
小服务程序
package myPack;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class RegServlet
*/
public class RegServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public RegServlet() {
super();
// 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 s1=request.getParameter("username");
System.out.println(s1);
String s2=request.getParameter("");//HERE I NEED THE PAssword value if PASS!==PASS2
System.out.println(s2);
String c="jdbc:mysql://localhost:3306/test";
Connection con=null;
System.out.println("Connection OK");
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
System.out.println("Done2");
con = DriverManager.getConnection(c, "root", "MyNewPass");
System.out.println("Done3");
PreparedStatement ps=null;
System.out.println("Done4");
String qs = "insert into userinfo values(?,?);";
ps = con.prepareStatement(qs);
ps.setString(1,s1);
ps.setString(2,s2);
System.out.println("Success");
ps.execute();
con.close();
}
catch (Exception e) {
System.out.println("Failed: " + e.toString());
// 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
}
}