Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
186 views
in Technique[技术] by (71.8m points)

jsp - printing servlet request attributes with expression language

I want to print servlet request attributes within a JSP file use expression language. I have done the following, in my Servlet I set all the necessary request attributes with

setRequestAttributes(String, Object);

In my JSP page I use the following line to print the attribute:

<c:out value="${string}"/>

After reading other posts I checked if version 2.4 is used in the web XML, it is.

When using getAttribute(String)

In the Servlet itself it prints the value just fine. Does anyone have any idea of what I need to do so I can make this work.

EDIT

Servlet code:

package servlets;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import classes.DBConnection;

public class MessageDetail extends HttpServlet {

    private static final long serialVersionUID = 1L;
    private String messageid;
    private Connection connect;

    public MessageDetail() {

        super();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

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

        connect = new DBConnection().returnConnection();
        messageid = request.getParameter("xxxxx");

        try {
            handleRequest(request);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        RequestDispatcher dispatcher = request.getRequestDispatcher("messageDetail.jsp");
        dispatcher.forward(request, response);
    }

    private void handleRequest(HttpServletRequest request) throws SQLException{

        ResultSet messageData = getMessageData("xxxxxx");
        ResultSet attachmentData = getMessageData("xxxxxx");
        Map<Integer, List<List<Object>>> messageMap = convertResultSetToMap(messageData);
        Map<Integer, List<List<Object>>> attachmentMap = convertResultSetToMap(attachmentData);
        Map<Integer, List<List<Object>>> totalMap = combineMaps(messageMap, attachmentMap);
        setRequestAttributes(request, totalMap);

    }

    private ResultSet getMessageData(String tableName) throws SQLException{

        String sql = "SELECT * FROM " + tableName + " WHERE xxxxxxxx= "+ xxxxxxx;
        PreparedStatement prestm = connect.prepareStatement(sql, ResultSet.CONCUR_READ_ONLY, ResultSet.TYPE_SCROLL_SENSITIVE);
        ResultSet rs = prestm.executeQuery();
        return rs;
    }

    private Map<Integer, List<List<Object>>> convertResultSetToMap(ResultSet rs) throws SQLException{

        Map <Integer, List<List<Object>>> resultSetMap = new HashMap<Integer, List<List<Object>>>(); 
        ResultSetMetaData rsmd = rs.getMetaData();
        int colTotal = rsmd.getColumnCount();
        int rowNumber = 1;

        rs.beforeFirst();
        while(rs.next()){

            List<List<Object>> row = new ArrayList<List<Object>>();
            for(int colCounter = 1; colCounter<=colTotal; colCounter++){

                List<Object> cellData = new ArrayList<Object>();

                cellData.add(rsmd.getColumnName(colCounter));
                cellData.add(rs.getObject(colCounter));

                row.add(cellData);
            }

            resultSetMap.put(rowNumber, row);
        }
        return resultSetMap;
    }

    private void setRequestAttributes(HttpServletRequest request, Map<Integer, List<List<Object>>> resultSetMap){

        for(Map.Entry<Integer, List<List<Object>>> entry : resultSetMap.entrySet()){
            List<List<Object>> rowData = entry.getValue();
            for(List<Object> listItem : rowData){

                request.setAttribute((String) listItem.get(0), listItem.get(1));
            }
        }
    }

    private Map <Integer, List<List<Object>>> combineMaps(Map <Integer, List<List<Object>>> messageData, Map <Integer, List<List<Object>>> attachmentData){
        Map <Integer, List<List<Object>>> totalMap = new HashMap<Integer, List<List<Object>>>();
        totalMap.putAll(messageData);
        totalMap.putAll(attachmentData);
        return totalMap;
    }
}

JSP code:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link href="/ArchiveSearch/resources/css/messageDetail.css" rel="stylesheet" type="text/css">

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Message detail</title>
</head>
<body>
    <div class="wrapper">
            <h1>Message Details</h1>
            <div class="messageMeta1">
                    <div><span class="label">Sender:</span><c:out value="${messageid}"/></div>
                    <div><span class="label">Receiver:</span></div>
                    <div><span class="label">Subject:</span></div><c:out value="${messagesender}"/>
                    <div><span class="label">Attachments:</span></div>
            </div>
            <div class="messageMeta2">
                    <div><span class="label">Case ID:</span></div>
                    <div><span class="label">Date:</span></div>
            </div>
            <div class="messageContent">
            </div>
    </div>
</body>
</html>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There's too much wrong in this code to give one suitable answer. Basically everything needs to be rewritten. You may find the kickoff examples in the following answer helpful: Show JDBC ResultSet in HTML in JSP page using MVC and DAO pattern.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...