繁体   English   中英

使用JSP页面而不是Java Servlet

[英]Using jsp page instead of java servlet

我已经使用HTML页面和Java Servlet类创建了登录页面。 但是我想知道是否有可能使用JSP而不是Java文件? 因此必须在Java类中将此代码使用到JSP页面中,并且仍然具有相同的功能吗?

<form action="Lognn" method="post"> 

<input  type="text" name="name"/>
<input type="text"name="pass"/>

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);

      }

通常,每个JSP都会翻译并编译为一个servlet,并且您可以使用servlet进行很多事情,而您可以使用JSP进行。

您可以将表单action =“ Lognn” method =“ post”更改为form action =“ Lognn.jsp” method =“ post”,然后

在Lognn.jsp中,您可以从html中读取输入参数,例如<%= request.getParameter(“ name”)%>,然后

在Lognn.jsp中,您可以直接连接到数据库,也可以使用EJB

最后,您可以从同一JSP提供html输出。

它可以在没有servlet的情况下正常运行,但是在CATALINA_HOME / WORk目录中,将在内部从您的JSP创建和编译servlet。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM