作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在发送 flag=update 以使用 URL 调用更新方法,同时发送用户想要编辑记录的当前记录的 ID。我的问题是无法从 edit 方法 获取参数(fname、lname、email、pass、cno)值。每次它从更新方法返回 null 时。
我认为更新方法或编辑方法有问题。请调查此事。我想了解为什么会出现此问题,因为我在过去几个小时内一直在尝试。
我知道我可以使用 request.getAttributes();
而不是 request.getParameter();
在同一个 servlet 中获取数据,但请告诉我他们是否有没有简单的方法?请帮助解决我的问题。提前致谢。
Registration.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Registration Page</title>
</head>
<body>
<form action="Controller1" >
First Name : <input type="text" name="fName"><br/>
Last Name : <input type="text" name="lName"><br/>
Email : <input type="text" name="eMail"><br/>
Password : <input type="password" name="password"><br/>
Contact No : <input type="text" name="cNo"><br/><br/>
<input type="submit" value="Register">
<input type="hidden" name="flag" value="register">
<a href="Controller1?flag=login">Login</a>
</form>
</body>
</html>
Login.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Login Page</title>
</head>
<body>
<form action="Controller1">
Email : <input type="text" name="lin_Email" value="insert your registered EmailID"><br/>
Password : <input type="text" name="lin_Pass" value="enter your registered password"><br/>
<input type="submit" value="Login">
<input type="hidden" name="flag" value="loginValidate">
</form>
</body>
</html>
Controller1.java
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Controller1
*/
@WebServlet("/Controller1")
public class Controller1 extends HttpServlet {
private static final long serialVersionUID = 1L;
int id;
String fname, lname, email, pass, cno, flag;
PrintWriter out;
/**
* @see HttpServlet#HttpServlet()
*/
public Controller1() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
flag = request.getParameter("flag");
if (flag.equals("register")) {
register(request, response);
} else if (flag.equals("login")) {
response.sendRedirect("/JDBC_Registration/Login.html");
}
else if (flag.equals("loginValidate")) {
loginValidate(request, response);
} else if (flag.equals("view")) {
try {
view(request, response);
} catch (SQLException e) {
e.printStackTrace();
}
} else if (flag.equals("edit")) {
try {
edit(request, response);
} catch (SQLException e) {
e.printStackTrace();
}
} else if (flag.equals("delete")) {
delete(request, response);
}
else if (flag.equals("update")) {
try {
update(request, response);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
private void update(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException, ServletException {
response.setContentType("text/html");
out = response.getWriter();
id = Integer.parseInt(request.getParameter("id"));
//
// String fname = request.getParameter("t1");
// String lname = request.getParameter("t2");
// String email = request.getParameter("t3");
// String pass = request.getParameter("t4");
// String cno = request.getParameter("t5");
fname = (String) request.getAttribute("t1");
lname = (String) request.getAttribute("t2");
email = (String) request.getAttribute("t3");
pass = (String) request.getAttribute("t4");
cno = (String) request.getAttribute("t5");
System.out.println("updated values... "+fname+" "+lname+" "+email+" "+pass+" "+cno);
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test2","root","root");
Statement stm = con.createStatement();
stm.executeUpdate("update registration set FirstName='"+fname+"',LastName='"+lname+"',Email='"+email+"',Password='"+pass+"',ContactNo='"+cno+"' where id='"+id+"'");
view(request, response);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/html");
out = response.getWriter();
id = Integer.parseInt(request.getParameter("id"));
System.out.println("delete record ID....."+id);
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test2","root","root");
Statement stm = con.createStatement();
stm.executeUpdate("delete from registration where id='"+id+"'");
System.out.println("successfully deleted...record ID : "+id);
view(request, response);
}catch (Exception e) {
System.out.println(e);
}
}
private void edit(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException {
response.setContentType("text/html");
out = response.getWriter();
id = Integer.parseInt(request.getParameter("id"));
System.out.println(""+id);
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test2","root","root");
Statement stm = con.createStatement();
ResultSet rs = stm.executeQuery("select * from registration where id='"+id+"'");
out.println("<form action='Controller1'><table border=2>");
out.println(
"<tr><td>ID</td><td>FirstName</td><td>LastName</td><td>Email</td><td>Password</td><td>ContactNo</td><td>Action</td></tr>");
while (rs.next()) {
out.println("<tr><td>" + rs.getString(1) + "</td><td><input type='text' name='t1' value=" + rs.getString(2) + "></td><td><input type='text' name='t2' value=" + rs.getString(3) + "></td><td><input type='text' name='t3' value=" + rs.getString(4) + "></td><td><input type='text' name='t4' value=" + rs.getString(5) + "></td><td><input type='text' name='t5' value=" + rs.getString(6) + "></td><td> <a href=Controller1?flag=update&id=" + rs.getString(1)
+ ">Update</a> </td></tr>");
}
out.println("</table></form>");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
private void view(HttpServletRequest request, HttpServletResponse response)
throws SQLException, IOException, ServletException {
response.setContentType("text/html");
out = response.getWriter();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test2", "root", "root");
Statement stmt = con.createStatement();
// RequestDispatcher rd =
// request.getRequestDispatcher("Welcome.html");
// rd.include(request, response);
ResultSet rs = stmt.executeQuery("select * from registration");
out.println("<form><table border=2>");
out.println(
"<tr><td>ID</td><td>FirstName</td><td>LastName</td><td>Email</td><td>Password</td><td>ContactNo</td><td>Action</td></tr>");
while (rs.next()) {
out.println("<tr><td>" + rs.getString(1) + "</td><td>" + rs.getString(2) + "</td><td>" + rs.getString(3)
+ "</td><td>" + rs.getString(4) + "</td><td>" + rs.getString(5) + "</td><td>" + rs.getString(6)
+ "</td><td> <a href=Controller1?flag=edit&id=" + rs.getInt(1)
+ ">Edit</a> <a href=Controller1?flag=delete&id=" + rs.getInt(1)
+ ">Delete</a> </td></tr>");
}
out.println("</table></form>");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
private void loginValidate(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/html");
out = response.getWriter();
try {
email = request.getParameter("lin_Email");
pass = request.getParameter("lin_Pass");
System.out.println("" + email + pass);
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test2", "root", "root");
Statement stmt = con.createStatement();
ResultSet rs = stmt
.executeQuery("select * from registration where Email='" + email + "'and Password='" + pass + "'");
if (!rs.next()) {
out.println("<h4 style='color:red'>Please Enter correct Email ID & Password...sq</h4><br/><br/>");
// response.sendRedirect("/JDBC_Registration/Login.html");
RequestDispatcher rd = request.getRequestDispatcher("Login.html");
rd.include(request, response);
} else {
// System.out.println("else if called....");
out.println("<h1>Welcome User :</h1><h3>" + rs.getString(2)
+ "</h3> <a href=Controller1?flag=view>view</a>");
RequestDispatcher rd = request.getRequestDispatcher("Welcome.html");
rd.include(request, response);
}
} catch (Exception e) {
System.out.println(e);
}
}
private void register(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/html");
out = response.getWriter();
try {
fname = request.getParameter("fName");
lname = request.getParameter("lName");
email = request.getParameter("eMail");
pass = request.getParameter("password");
cno = request.getParameter("cNo");
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test2", "root", "root");
Statement stmt = con.createStatement();
stmt.executeUpdate("insert into registration(FirstName,LastName,Email,Password,ContactNo) values('" + fname
+ "','" + lname + "','" + email + "','" + pass + "','" + cno + "')");
System.out.println(" INSERT : : : " + fname + " " + lname + " " + email + " " + pass + " " + cno);
response.sendRedirect("/JDBC_Registration/Login.html");
// view(request, response);
} catch (Exception e) {
System.out.println(e);
}
}
}
最佳答案
当我用提交按钮而不是 URL 来调用更新方法时,它对我有用。
<input type='submit'>
<input type='hidden' name='flag' value='update'>
<input type='hidden' name='id' value='"+id+"'>
这里发生了什么,超链接值没有附加其余参数 t1、t2、t3、t4 和 t5。通过放置提交按钮,它会将所有参数视为默认 get() 方法。
关于java - 无法使用 request.getParameter() 或 request.getAttributes() 从同一个 Servlet 中检索数据,尝试都只获取 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42330281/
我是一名优秀的程序员,十分优秀!