[英]Using jsp page instead of java servlet
I have created a login page using HTML page and Java Servlet class. 我已经使用HTML页面和Java Servlet类创建了登录页面。 However I am wondering if it is possible to have a JSP instead of the Java file? 但是我想知道是否有可能使用JSP而不是Java文件? So essential use this code in the Java class into a JSP page and still have the same functionality? 因此必须在Java类中将此代码使用到JSP页面中,并且仍然具有相同的功能吗?
<form action="Lognn" method="post">
<input type="text" name="name"/>
<input type="text"name="pass"/>
Java class Java类
Public class Login extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
String name = request.getParameter("name");
String pass = request.getParameter("pass");
MyDb1 db = new MyDb1();
Connection con = db.getCon();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select uid,name,pass from register where email = '"+name+"' and pass = '"+pass+"'");
while ((rs.next())) {
String uid = rs.getString("uid");
//out.println("User id"+uid);
HttpSession session=request.getSession();
session.setAttribute("name",uid);
response.sendRedirect("http://localhost:8080/Final/userprofile.jsp");
}
} catch (SQLException ex) {
Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
}
Generally, each JSP is translated and compiled to a servlet, and a lot of things you can do with servlets you can do with JSP. 通常,每个JSP都会翻译并编译为一个servlet,并且您可以使用servlet进行很多事情,而您可以使用JSP进行。
You can change your form action="Lognn" method="post" to form action="Lognn.jsp" method="post", then 您可以将表单action =“ Lognn” method =“ post”更改为form action =“ Lognn.jsp” method =“ post”,然后
in Lognn.jsp you can read your input parameters from html like <%= request.getParameter("name")%>, then 在Lognn.jsp中,您可以从html中读取输入参数,例如<%= request.getParameter(“ name”)%>,然后
in Lognn.jsp you can connect to a database directly or you can use EJB 在Lognn.jsp中,您可以直接连接到数据库,也可以使用EJB
and finally you can give html output back from the same JSP. 最后,您可以从同一JSP提供html输出。
It formally will work without servlet, but in CATALINA_HOME/WORk directory a servlet will be internally created and compiled from your JSP. 它可以在没有servlet的情况下正常运行,但是在CATALINA_HOME / WORk目录中,将在内部从您的JSP创建和编译servlet。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.