0

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>
TrashCan
  • 817
  • 4
  • 13
  • 20
  • After some searching with a clear head I find the the following information. http://www.exampledepot.com/egs/javax.servlet/State.html When the request is complete this attributes are destroyed. – TrashCan Jun 14 '12 at 08:43
  • Show us the real code where you set the request attribute. Do you really chose "string" as the name of the attribute? – JB Nizet Jun 14 '12 at 08:44
  • After some searching with a clear head I find the the following information. http://www.exampledepot.com/egs/javax.servlet/State.html When the request is complete this attributes are destroyed. When exactly is a request "complete"? When the request is delivered to the servlet or after the servlet is done processing the request. If the second option is true than I can use the request attributes within the entire doPost or doGet method, correct? (If not please explain.) – TrashCan Jun 14 '12 at 08:56
  • The information found at the mentioned site also says that there are two other ways to store information in a Servlet via context or a session, session is clear I guess (saving usernames, passwords, etc.) What is context used for? – TrashCan Jun 14 '12 at 08:56
  • Nowhere in the servlet do you call getAttribute(), so it's impossible to say if your attribute names are correct or not. All your attribute names are dynamic, so it's impossible to say what they are by reading your code. And you should use objects rather than Map>>. Your code is unmaintainable. Use a debugger to know which attribute names are set in the request. – JB Nizet Jun 14 '12 at 09:10
  • How would you save a ResultSet in a datastructure? I once saw an example where they used a Map + LinkedList. I agree that I need to use something like a row object where all the row information is saved. Do you have example? To set the request attributes, I need to save the columnname and the value. (for the attribute name and the value.) I also checked the parameters, I turns out there is only one attribute saved. So I need to check that out. – TrashCan Jun 14 '12 at 09:32
  • The rows in the table represent something functional: a User, an Order, a Supplier, a Customer, whatever. This is the kind of objects that should be returned by the query, and that you should manipulate in your servlet and in your JSP. – JB Nizet Jun 14 '12 at 10:12

1 Answers1

1

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.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks for the link, and the tutorial referenced at that post. Looks very nice. And I will definitely do a little project with it, to see if I get it. Thing is, I'm new to web development, I did some little things with servlets and JSP but that's about it. So there is alot of new stuff floating around in my head right now. – TrashCan Jun 15 '12 at 09:22
  • for example I've been frustrating over this: ` $('#searchResults tbody tr').live('dblclick', function () { var aData = table.fnGetData( this,0 ); $.ajax({ url: "MessageDetail", type: "POST", async: false, dataType: 'html', data: "messageid=" + aData, succes: function(data) { console.log(data); }, error: function(msg) { console.log(msg); } }); }); ` I dont get why I dont get any console logging from this function. – TrashCan Jun 15 '12 at 09:43