Click here to Skip to main content
16,008,010 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
im working on my graduation project...... using jsp and servlet...
My problem is::;

i have a servlet which must select from database table and return it in resultset
then in my code i add the results to a list and put it in session.....

in jsp page.... i want to print the values from list in table.;but this is not happend

XML
<%--
    Document   : viewmyprojects
    Created on : 14/06/2012, 07:34:34 م
    Author     : mamer
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page language="java" import="java.util.*" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
      <FORM ENCTYPE="multipart/form-data" ACTION="ViewMyProjects" METHOD="POST">
<!-- Start: page-top-outer -->
<div id="page-top-outer">

<!-- Start: page-top --><!-- End: page-top -->

</div>
<!-- End: page-top-outer -->

<div class="clear">&nbsp;</div>

<!--  start nav-outer-repeat................................................................................................. START --><!--  start nav-outer-repeat................................................... END -->

 <div class="clear"></div>

<!-- start content-outer ........................................................................................................................START -->
<div id="content-outer">
<!-- start content -->
<div id="content">

    <!--  start page-heading -->
    <div id="page-heading">

     <h1>Your Projects:</h1>
     <p>&nbsp;</p>
    </div>
    <!-- end page-heading -->

    <table border="0" cellpadding="0" cellspacing="0"  id="id-form">
            <%
            List result = ((List)session.getAttribute("projectsList"));
            for(int i=0; i< result.size()-1 ;i++){%>
            <tr>
                <td>
                    <% result.get(i); %>
                </td>
            </tr>
            <tr>
                <td>
                    <% result.get(i+1); %>
                </td>
            </tr>
            <%}%>
        </table>
</form>
    </body>
</html>

package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import java.io.*;
import java.util.*;


/**
 *
 * @author mamer
 */
@WebServlet(name = "ViewMyProjects", urlPatterns = {"/ViewMyProjects"})
public class ViewMyProjects extends HttpServlet {
    
    ResultSet rs;
String pname;
  String desc;
  
    int userId;
    List list=new ArrayList();
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
       
         String uId = request.getSession(true).getAttribute("uid").toString();
       int userId = Integer.parseInt(uId);
      
        
        
       System.out.println(userId+"...........");
       try {
       
       
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            Connection connection = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/autodoc","root","root");
              String query="select name,description from projects where id='"+userId+"'";
            Statement stmt;
           stmt=
  connection.createStatement();
   stmt.executeQuery(query);
   rs=stmt.getResultSet();
 
            
            while(rs.next()){
               
             pname=rs.getString(1);
             desc=rs.getString(2);
             list.add(pname);
             list.add(desc);
          
            }
            rs.close();
            stmt.close();
            
            request.getSession(true).setAttribute("projectsList", list);
            request.getRequestDispatcher("/viewmyprojects.jsp").forward(request, response);
           
            
            System.out.println(pname+"..........."+desc);
            out.println("<a href=\"http://localhost:8080/WebApplication43\\projects/"+pname+"\">"+pname+"</a>");
       }
            catch(Exception ex) {
            System.out.println(ex.toString());
            System.out.println("error");
        }
        finally {            
            out.close();
        }
        
        
        
        
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}








can anybody help me.......... Or give me an example


Thanx In Advance
Posted
Comments
Sergey Alexandrovich Kryukov 15-Jun-12 17:27pm    
"Is not happend" is not informative. You need to explain your code and what's going wrong properly. Did you try to debug it?
--SA

1 solution

You have two methods in your code doGet and doPost.

Your doGet method does nothing. All your code is in the doPost. This method is only called when the page has been posted to.

Are you sure the page has received a post? As I can't see your post method actually use any post values.

On a side note:

String query="select name,description from projects where id='"+userId+"'";

This approach to populating parameters in an SQL statement is not advisable and leaves you open to SQL injection attacks. All values passed into SQL statements should be done via parameters.

Also based on the way you add values to your list and the single value increase in your rendering loop (i++) I expect your rendered table will look like this:

Value A, Value B
Value B, Value C
Value C, Value D
Value D, Value E


Hope this helps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900