jsp and getting attributes data

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

30 Replies - 1312 Views - Last Post: 02 December 2015 - 12:06 AM

#1 zerocool18   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 186
  • Joined: 12-October 12

jsp and getting attributes data

Posted 30 November 2015 - 08:27 PM

hello , I am trying to display the note attribute that I saved into my request.setAttribute("entries", entries), but Imhaving trouble with getting 1 note. i know you can use a for each loop to cycle through my list and pull out everything i need. but that would return all the notes for that user. I only want to display 1 note. how can i do this?

like i said above , i know i can get the info with this

<table border=1>

<tr><th>Titles</th></tr>
<c:forEach items="${entries}" var="entry">
<tr>
<td><a href="NoteDetails.jsp">${entry.notes}</a></td>
</tr>
</c:forEach>

</table>

but if the user has more then 1 note in the database , itll return them all. i only want to display 1 note. how can i do this?






mynotes controller
@WebServlet("/Homework2/MyNotes")
public class MyNotes extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	public void init() throws ServletException
    {
		
		if (getServletContext().getAttribute("cs320.hw2.Note") == null)
			getServletContext().setAttribute("cs320.hw2.Note", new ArrayList<Notes>());

       

        try
        {
            Class.forName( "com.mysql.jdbc.Driver" );
        }
        catch( ClassNotFoundException e )
        {
            throw new ServletException( e );
        }
    }

	@SuppressWarnings("unchecked")
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
			getServletContext().setAttribute("cs320.hw2.Note", new ArrayList<Notes>());

		HttpSession session = request.getSession();
		String user = (String) session.getAttribute("CurrentUser");
		List<Notes> entries = (List<Notes>) getServletContext().getAttribute(
				"cs320.hw2.Note");

		if (user == null) {
			// User isn't authenticated
			response.sendRedirect("Login");
			return;
		}
		else {
			
			
			 Connection c = null;
		        try
		        {
		            String host = "";
		            String port = "";
		            String dbName = "";
		            String username = ";
		            String password = "";
		            
		            String url = "jdbc:mysql://" + host + ":" + port + "/" + dbName;

		            c = DriverManager.getConnection( url, username, password );
		            Statement stmt = c.createStatement();
		            String sname = (String)session.getAttribute("name");
		            String selectStatement = "select * from entry where name =?";
		            PreparedStatement prepStmt = (PreparedStatement) c.prepareStatement(selectStatement);
		            prepStmt.setString(1, sname);
		            ResultSet rs = prepStmt.executeQuery();
		      
		            while( rs.next() )
		            {
		            		int id 			= rs.getInt("id");
		            		String name 	= rs.getString("name");
		            		String note 	= rs.getString("note");
		            		String title 	= rs.getString("title");
		            		
		            		Notes entry = new Notes(id, name, note, title);
		            		entries.add(entry);
		            }
		          
		            request.setAttribute("entries", entries);
		           
		            request.getRequestDispatcher( "/WEB-INF/homework2/MyNotes.jsp" ).forward(
				         request, response );
		            
		            for (Notes u : entries) {
						System.out.println("id " + u.id + "name:  " + u.name + "Note:  "
								+ u.note + "title:  " + u.title);
					}
		        }
		        catch( SQLException e )
		        {
		            throw new ServletException( e );
		        }
		        finally
		        {
		            try
		            {
		                if( c != null ) c.close();
		            }
		            catch( SQLException e )
		            {
		                throw new ServletException( e );
		            }
		        }


			}	
			
	}

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

	}

}






mynotes jsp
[code


<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ 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" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>MyNotes</title>
</head>
<body>

<p align="right">Hello, ${sessionScope.CurrentUser}!&nbsp;&nbsp;&nbsp;<a href="Logout">Logout</a></p>
<span>JOT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="MyNotes">My Notes</a>&nbsp;|&nbsp;<a href="NewNote.jsp">New</a></span>
<br>
<br>
<br>


<table border=1>

<tr><th>Titles</th></tr>
<c:forEach items="${entries}" var="entry">
<tr>
<td><a href="NoteDetails.jsp">${entry.title}</a></td>
</tr>
</c:forEach>

</table>


<p><a href="NewNote.jsp">New Note</a></p>

<p>${note} </p>

</body>
</html>


[/code]

Is This A Good Question/Topic? 0
  • +

Replies To: jsp and getting attributes data

#2 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 976
  • View blog
  • Posts: 3,849
  • Joined: 13-June 14

Re: jsp and getting attributes data

Posted 30 November 2015 - 08:53 PM

I don't know whether JSTL allows you to do that, so you could consult the documentation to find out. Having said that, you could instead make the decision earlier - only pass the collection containing the one element you want to display to the view.
Was This Post Helpful? 0
  • +
  • -

#3 zerocool18   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 186
  • Joined: 12-October 12

Re: jsp and getting attributes data

Posted 30 November 2015 - 09:10 PM

im not quite sure i understand what you mean. you mean like in the controller? before i pass it to the view (jsp)?
Was This Post Helpful? 0
  • +
  • -

#4 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 976
  • View blog
  • Posts: 3,849
  • Joined: 13-June 14

Re: jsp and getting attributes data

Posted 30 November 2015 - 09:14 PM

Yes, exactly.
Was This Post Helpful? 0
  • +
  • -

#5 zerocool18   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 186
  • Joined: 12-October 12

Re: jsp and getting attributes data

Posted 30 November 2015 - 09:25 PM

alright , so bare with me lol. to only pass the collection containing the one element i want, which is the note string that i read in from the database. would i be using request.setAttribute?
Was This Post Helpful? 0
  • +
  • -

#6 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 976
  • View blog
  • Posts: 3,849
  • Joined: 13-June 14

Re: jsp and getting attributes data

Posted 30 November 2015 - 09:28 PM

Without meaning to sound rude, what do you think? This is a genuine question to get you to think about what you're doing and why.

Also, if you're only ever going to pass one item to the view, then obviously you don't need the collection - I just thought you had cases where sometimes you wanted to pass one and other times, you wanted to pass more than one.

This post has been edited by ndc85430: 30 November 2015 - 09:28 PM

Was This Post Helpful? 0
  • +
  • -

#7 zerocool18   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 186
  • Joined: 12-October 12

Re: jsp and getting attributes data

Posted 30 November 2015 - 11:15 PM

i appreciate the help, but i feel like ive hit the wall with this. im totally confused on what i am doin.
Was This Post Helpful? 0
  • +
  • -

#8 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 976
  • View blog
  • Posts: 3,849
  • Joined: 13-June 14

Re: jsp and getting attributes data

Posted 30 November 2015 - 11:17 PM

OK. Let's start again, what are you trying to do?
Was This Post Helpful? 0
  • +
  • -

#9 zerocool18   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 186
  • Joined: 12-October 12

Re: jsp and getting attributes data

Posted 30 November 2015 - 11:24 PM

ok , im connecting to a database . extracting the data from the table (name,id,note, and title). im trying to display the note part of my data in my jsp page so i can see the notes. but instead of looping through a for each loop and getting all the notes from a particular user, i just want to display note at a time.
Was This Post Helpful? 0
  • +
  • -

#10 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 976
  • View blog
  • Posts: 3,849
  • Joined: 13-June 14

Re: jsp and getting attributes data

Posted 30 November 2015 - 11:26 PM

So, in your controller, can you extract the single note you want from the database, instead of all of them?
Was This Post Helpful? 0
  • +
  • -

#11 zerocool18   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 186
  • Joined: 12-October 12

Re: jsp and getting attributes data

Posted 30 November 2015 - 11:29 PM

yes in my controller , ive connected to the database and extracted all the data, added the entry , and saved it to my request, request.setAttribute("entries", entries);

this is my controller


@WebServlet("/Homework2/NoteDetails")
public class NoteDetails extends HttpServlet {
	private static final long serialVersionUID = 1L;
	public void init() throws ServletException
    {
		
		if (getServletContext().getAttribute("cs320.hw2.Note") == null)
			getServletContext().setAttribute("cs320.hw2.Note", new ArrayList<Notes>());

       

        try
        {
            Class.forName( "com.mysql.jdbc.Driver" );
        }
        catch( ClassNotFoundException e )
        {
            throw new ServletException( e );
        }
    }
	

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		getServletContext().setAttribute("cs320.hw2.Note", new ArrayList<Notes>());

		HttpSession session = request.getSession();
		String user = (String) session.getAttribute("CurrentUser");
		List<Notes> entries = (List<Notes>) getServletContext().getAttribute(
				"cs320.hw2.Note");
	    
	    if (user == null) {
			// User isn't authenticated
			response.sendRedirect("Login");
			return;
		}
		else {
			
			
			 Connection c = null;
		        try
		        {
		            String host = "";
		            String port = "";
		            String dbName = "";
		            String username = "";
		            String password = ";
		            
		            String url = "jdbc:mysql://" + host + ":" + port + "/" + dbName;

		            c = DriverManager.getConnection( url, username, password );
		            
		            String sname = (String)session.getAttribute("name");
		            String selectStatement = "select * from entry where name =?";
		            PreparedStatement prepStmt = (PreparedStatement) c.prepareStatement(selectStatement);
		            prepStmt.setString(1, sname);
		            ResultSet rs = prepStmt.executeQuery();
		      
		            while( rs.next() )
		            {
		            		int id 			= rs.getInt("id");
		            		String name 	= rs.getString("name");
		            		String note 	= rs.getString("note");
		            		String title 	= rs.getString("title");
		            		
		            		Notes entry = new Notes(id, name, note, title);
		            		entries.add(entry);
		            		 
		            }
		          
		            request.setAttribute("entries", entries);
       	            request.getRequestDispatcher( "/WEB-INF/homework2/NoteDetails.jsp" ).forward(
				         request, response );
		            
		   
		            
		            
		        }
		        catch( SQLException e )
		        {
		            throw new ServletException( e );
		        }
		        finally
		        {
		            try
		            {
		                if( c != null ) c.close();
		            }
		            catch( SQLException e )
		            {
		                throw new ServletException( e );
		            }
		        }


			}	
			
	}

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

}





Was This Post Helpful? 0
  • +
  • -

#12 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 976
  • View blog
  • Posts: 3,849
  • Joined: 13-June 14

Re: jsp and getting attributes data

Posted 30 November 2015 - 11:31 PM

You said you only wanted to display one entry, so why are you selecting all of them for that user?
Was This Post Helpful? 0
  • +
  • -

#13 zerocool18   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 186
  • Joined: 12-October 12

Re: jsp and getting attributes data

Posted 30 November 2015 - 11:37 PM

View Postndc85430, on 30 November 2015 - 11:31 PM, said:

You said you only wanted to display one entry, so why are you selecting all of them for that user?


i still need to display a table of the titles of the notes, that are links that connect to the a page that displays the whole note. i also need to display there name at the top. so when i click a title , it goes to the controller im working on now , and forwards it to the jsp page that displays the note.
Was This Post Helpful? 0
  • +
  • -

#14 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 976
  • View blog
  • Posts: 3,849
  • Joined: 13-June 14

Re: jsp and getting attributes data

Posted 30 November 2015 - 11:40 PM

I'm confused as to what your question is, then. If you're really asking, "how do I get one row from the database?", then think about how you can modify your query to do that. How does one uniquely identify a row in a table?
Was This Post Helpful? 0
  • +
  • -

#15 zerocool18   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 186
  • Joined: 12-October 12

Re: jsp and getting attributes data

Posted 30 November 2015 - 11:42 PM

View Postndc85430, on 30 November 2015 - 11:40 PM, said:

I'm confused as to what your question is, then. If you're really asking, "how do I get one row from the database?", then think about how you can modify your query to do that. How does one uniquely identify a row in a table?


the id (facepalm)
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3