サーブレットを使用して登録フォームから詳細を取得し、既存のすべてのユーザーを表示しようとしています。私はサーブレットでうまくいかないことを知っています。本当にどこにあるのかわかりません。
これは私のreg.jspです
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Registration</title>
</head>
<body>
<form method="post" action="UserController.java">
<center>
<table border="1" width="30%" cellpadding="5">
<thead>
<tr>
<th colspan="2">Enter Information Here</th>
</tr>
</thead>
<tbody>
<tr>
<td>First Name</td>
<td><input type="text" name="fname" value="" /></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type="text" name="lname" value="" /></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email" value="" /></td>
</tr>
<tr>
<td>User Name</td>
<td><input type="text" name="uname" value="" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="pass" value="" /></td>
</tr>
<tr>
<td><input type="submit" value="Submit" /></td>
<td><input type="reset" value="Reset" /></td>
</tr>
<tr>
<td colspan="2">Already registered!! <a href="index.jsp">Login Here</a></td>
</tr>
</tbody>
</table>
</center>
</form>
</body>
</html>
これは私のサーブレットコードです
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.SwingUtilities;
import com.shreya.dao.UserDao;
import com.shreya.model.Details;
import com.shreya.model.User;
public class UserController extends HttpServlet {
private static final long serialVersionUID = 1L;
private static String INSERT_OR_EDIT = "/user.jsp";
private static String LIST_USER = "/listUser.jsp";
private UserDao dao;
public UserController() {
super();
dao = new UserDao();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String forward="";
String act = request.getParameter("act");
if (act != null && !act.equalsIgnoreCase("null") && act.equalsIgnoreCase("login")) {
forward= "/Login.jsp";
}
else if (act != null && !act.equalsIgnoreCase("null") && act.equalsIgnoreCase("delete"))
{
int userId = Integer.parseInt(request.getParameter("userId"));
dao.deleteUser(userId);
forward = LIST_USER;
request.setAttribute("users", dao.getAllUsers());
} else if (act!=null && !act.equalsIgnoreCase("null") && act.equalsIgnoreCase("edit")){
forward = INSERT_OR_EDIT;
int userId = Integer.parseInt(request.getParameter("userId"));
User user1 = dao.getUserById(userId);
request.setAttribute("user", user1);
} else if (act!=null && !act.equalsIgnoreCase("null") && act.equalsIgnoreCase("listUser")){
forward = LIST_USER;
request.setAttribute("users", dao.getAllUsers());
} else
forward = "/Login.jsp";
RequestDispatcher view = request.getRequestDispatcher(forward);
view.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
User user = new User();
Details details = new Details();
user.setFirstName(request.getParameter("firstName"));
user.setLastName(request.getParameter("lastName"));
details.setUsername(request.getParameter("username"));
details.setPassword(request.getParameter("password"));
// details.setId(request.getParameter("id"));
String str=request.getParameter("username");
String str1=request.getParameter("password");
if(str.equalsIgnoreCase("shreya")&&str1.equalsIgnoreCase("singh"))
System.out.println("Login!");
else
{
System.out.println("Login failed!");
request.setAttribute("users",dao.getAllUsers());
request.getRequestDispatcher("/listUser.jsp").forward(request, response);
}
}
}
編集1:
else if (act!=null && !act.equalsIgnoreCase("null") && act.equalsIgnoreCase("register")){
forward = LIST_USER;
request.setAttribute("users", dao.getAllUsers());
doGet メソッドで値を保存するアクション レジスタを渡しました。dopost でそれに応じて変更するにはどうすればよいですか?