我正在开发一个基于Java的注册页面。索引页面有一个表单,该表单被填写后发布到本地数据库。截至目前提交时,它会显示一条消息,说明是否输入了数据。我不知道如何临时显示该消息,然后将其重定向到另一个页面。下面是我的Java Servlet。
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
/**
* Servlet implementation class Register
*/
@WebServlet("/Register")
public class Register extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Register() {
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
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id = request.getParameter("Id");
String first = request.getParameter("First");
String last = request.getParameter("Last");
String email = request.getParameter("Email");
String phone = request.getParameter("Phone");
User user = new User(id, first, last, email, phone);
RegisterDao rDao = new RegisterDao();
String result = rDao.insert(user);
DateBean dBean = new DateBean(); /** import bean **/
Date dateTime = dBean.getDateTime();
PrintWriter writer = response.getWriter();
writer.println(result + " " + dateTime);
writer.flush();
writer.close();
response.sendRedirect("index.jsp");
}
}
我做了一个变通。我在显示消息中添加了一个html链接,以便单击以将用户带回主页。