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

时间:2012-09-02 04:15:09

标签: 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 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);
            }
        }
    }
}

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>

现在,当我访问我的servlet类时,它会调用profile.jsp。但是,表单的文本字段显示编码中的EL代码。我很困惑,为什么这不起作用。

1 个答案:

答案 0 :(得分:0)

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