如何通过servlet在jsp中显示数据库中的数据?

如何通过servlet在jsp中显示数据库中的数据?,jsp,servlets,Jsp,Servlets,我正在使用Hibernate、SERVLET、JSP和MySql数据库开发一个应用程序。 我的项目中使用的表是BeanRegister 在这里,我面临着在jsp页面中使用EL的问题。在这个JSP页面中,我想显示从数据库表中获取的一些信息。这是我的密码 ControllerProfile.java: import java.io.IOException; import java.util.List; import javax.servlet.RequestDispatcher; import ja

我正在使用Hibernate、SERVLET、JSP和MySql数据库开发一个应用程序。 我的项目中使用的表是BeanRegister

在这里,我面临着在jsp页面中使用EL的问题。在这个JSP页面中,我想显示从数据库表中获取的一些信息。这是我的密码

ControllerProfile.java:

import java.io.IOException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.Query;

public class ControllerProfile extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        if ((request.getParameter("accsub") != null)) {
            if (request.getParameter("accuser") != null) {
                Session session = null;

                SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
                session = sessionFactory.openSession();
                org.hibernate.Transaction tx = session.beginTransaction();


                String i = request.getParameter("accuser");
                String Q = " from BeanRegister where userid= :id ";
                Query query = session.createQuery(Q);
                query.setParameter("id", i);

                List<BeanRegister> profile = query.list();
                request.setAttribute("profile", profile);

                tx.commit();
                session.flush();
                session.close();

                RequestDispatcher dispatcher = request.getRequestDispatcher("profile.jsp");
                dispatcher.forward(request, response);
            } else {
                System.out.println("error");

                RequestDispatcher dispatcher = request.getRequestDispatcher("viewprofile.jsp");
                dispatcher.forward(request, response);
            }
        }
    }
}
import java.io.IOException;
导入java.util.List;
导入javax.servlet.RequestDispatcher;
导入javax.servlet.ServletException;
导入javax.servlet.http.HttpServlet;
导入javax.servlet.http.HttpServletRequest;
导入javax.servlet.http.HttpServletResponse;
导入org.hibernate.Session;
导入org.hibernate.SessionFactory;
导入org.hibernate.cfg.Configuration;
导入org.hibernate.Query;
公共类ControllerProfile扩展了HttpServlet{
@凌驾
受保护的void doGet(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException{
}
@凌驾
受保护的void doPost(HttpServletRequest请求、HttpServletResponse响应)引发ServletException、IOException{
if((request.getParameter(“accsub”)!=null)){
if(request.getParameter(“原告”)!=null){
会话=空;
SessionFactory SessionFactory=新配置().configure().buildSessionFactory();
session=sessionFactory.openSession();
org.hibernate.Transaction tx=session.beginTransaction();
字符串i=request.getParameter(“原告”);
String Q=“from BeanRegister,其中userid=:id”;
Query Query=session.createQuery(Q);
query.setParameter(“id”,i);
List profile=query.List();
setAttribute(“profile”,profile);
tx.commit();
session.flush();
session.close();
RequestDispatcher=request.getRequestDispatcher(“profile.jsp”);
转发(请求、响应);
}否则{
System.out.println(“错误”);
RequestDispatcher=request.getRequestDispatcher(“viewprofile.jsp”);
转发(请求、响应);
}
}
}
}
Profile.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        ....
        ....
        <form name="proview" id="proview" action="updateprofile.jsp" method="post" >

            <div align="center">
                <table width="366" border="0" bgcolor="#3333CC">
                    <c:forEach var="pro" items="${profile}">
                        <tr>
                            <td width="171" height="23"></td>
                            <td width="194"><input name="password" id="password" type="hidden" value="${pro.password}"/></td>
                        </tr>
                        <tr>
                            <td width="171" height="23"><div align="center">NAME</div></td>
                            <td width="194"><input name="name" type="text" id="name" value="${pro.name}" readonly="readonly"/></td>
                        </tr>
                        <tr>
                            <td width="171" height="23"><div align="center">USER ID</div></td>
                            <td width="194"><input name="userid" id="userid" type="text" value="${pro.userid}" readonly="readonly"/></td>
                        </tr>
                        <tr>
                            <td width="171" height="23"><div align="center">ADDRESS</div></td>
                            <td><input type="text" name="address" id="address" value="${pro.address}" readonly="readonly"/></td>
                        </tr>
                        <tr>
                            <tr>
                                <td width="171" height="23"><div align="center">GENDER</div></td>
                                <td width="194"><input name="sex" type="text" id="sex" value="${pro.sex}" readonly="readonly"/></td>
                            </tr>
                        </tr>
                        <tr>
                            <td width="171" height="23"><div align="center">MOBILE NUMBER</div></td>
                            <td><input name="number" type="text" id="number" value="${pro.number}" readonly="readonly"/></td>
                        </tr>
                        <tr>
                            <td width="171" height="23"><div align="center">EMAIL ID</div></td>
                            <td><input name="email" id="email" type="text" value="${pro.email}" readonly="readonly"/></td>
                        </tr>

                        <tr>
                            <td width="171" height="23"></td>
                            <td width="185"><input name="password1" id="password1" type="hidden" value="${pro.password1}"/></td>
                        </tr>

                        <tr>
                            <td>
                                <div align="center">
                                    Want To Edit Ur Profile Click Here
                                </div></td>

                            <td >
                                <div align="center">
                                    <input type="submit" name="updateprof" id="updateprof" value="UPDATE" />
                                </div></td>
                        </tr>
                    </c:forEach>  
                </table>
            </div>
        </form>
    </body>
</html>

....
....
名称
用户ID
地址
性别
手机号码
电子邮件ID
要编辑您的个人资料,请单击此处
现在,当我访问servlet类时,它调用profile.jsp。但是,表单的文本字段显示编码中的EL代码。我很困惑为什么这不起作用

i think you use Tomcat 7
Tomcat 7 does not come with JSTL jar files by default.
   First, locate the jar files for both JSTL API and JSTL Implementation. They are located on the JSP Standard Tag Library download page. Click into each repository and find the .jar file.

Tomcat 7
           =>Servlet -api-3.0
           =>JSTL-1.2

 refer this link:http://javahelpersin.blogspot.in/2013_01_01_archive.html