Meaningless Drivel is fun!*
The moose likes JSP and the fly likes Passing a variable from JSP to Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Passing a variable from JSP to Servlet" Watch "Passing a variable from JSP to Servlet" New topic
Author

Passing a variable from JSP to Servlet

Priya Prasad
Ranch Hand

Joined: Jun 11, 2013
Posts: 31
I am trying to send the vaue of Af Number from the JSp page to the servlet in order to delete all the fields associated with it. Like this.

My Jsp Page

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
<%--
Document : response
Created on : Dec 22, 2009, 8:52:57 PM
Author : nbuser
--%>

<sql:query var="bkplQuery" dataSource="jdbc/erpl">
SELECT * FROM bkpl where bkpl.Af_Scheme_Number=? <sql:param value="${param.Af_Scheme_Number}"/>
</sql:query>

<c:set var="bkplDetails" value="${bkplQuery.rows[0]}"/>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link rel="stylesheet" type="text/css" href="style.css">
<title>Details of: ${bkplDetails.Af_Scheme_Number}</title>
</head>

<body>
<table>

<tr>
<th colspan="2">${bkplDetails.Af_Scheme_Details}:${bkplDetails.Af_Scheme_Number}</th>
</tr>
<tr>
<td><strong>Description: </strong></td>
<td><span style="font-size:smaller; font-style:italic;">${bkplDetails.Af_Scheme_Details}</span></td>
</tr>
<tr>
<td><strong>Af Scheme Type: </strong></td>
<td><strong>${bkplDetails.Af_Scheme_Type} </strong>
<br><span style="font-size:smaller; font-style:italic;">
<em>Granted By: ${bkplDetails.GrantedBy}</em></span></td>
</tr>
<tr>
<td><strong>Amount: </strong></td>
<td><span style="font-size:smaller; font-style:italic;">${bkplDetails.Amount}</span></td>
</tr>
<tr>
<td><strong>Approval Date: </strong></td>
<td><span style="font-size:smaller; font-style:italic;">${bkplDetails.Approval_Date}</span></td>
</tr>
<tr>
<td><strong>Completion Period: </strong></td>
<td><span style="font-size:smaller; font-style:italic;">${bkplDetails.Completion_Period}</span></td>
</tr>
<tr>
<td><strong>Original Completion Date: </strong></td>
<td><span style="font-size:smaller; font-style:italic;">${bkplDetails.Original_Completion_Date}</span></td>
</tr>
<tr>
<td><strong>Remarks: </strong></td>
<td><span style="font-size:smaller; font-style:italic;">${bkplDetails.Remarks}</span></td>
</tr>
<tr>
<td><strong>Modified AF Scheme Type: </strong></td>
<td><span style="font-size:smaller; font-style:italic;">${bkplDetails.Modified_Af_Scheme_Type}</span></td>
</tr>

<tr>
<td><strong>Modified GrantedBy: </strong></td>
<td><span style="font-size:smaller; font-style:italic;">${bkplDetails.Modified_GrantedBy}</span></td>
</tr>

<tr>
<td><strong>Modified Af Scheme Details: </strong></td>
<td><span style="font-size:smaller; font-style:italic;">${bkplDetails.Modified_Af_Scheme_Details}</span></td>
</tr>

<tr>
<td><strong>Modified Amount: </strong></td>
<td><span style="font-size:smaller; font-style:italic;">${bkplDetails.Modified_Amount}</span></td>
</tr>

<tr>
<td><strong>Modified Completion Period: </strong></td>
<td><span style="font-size:smaller; font-style:italic;">${bkplDetails.Modified_Completion_Period}</span></td>
</tr>

<tr>
<td><strong>Modified Remarks: </strong></td>
<td><span style="font-size:smaller; font-style:italic;">${bkplDetails.Modified_Remarks}</span></td>
</tr>

<tr>
<td><strong>Modified Original Completion Date: </strong></td>
<td><span style="font-size:smaller; font-style:italic;">${bkplDetails.Modified_Original_Completion_Date}</span></td>
</tr>
<tr>
<td><strong>Modified Approval Date: </strong></td>
<td><span style="font-size:smaller; font-style:italic;">${bkplDetails.Modified_Approval_Date}</span></td>
</tr>
<% request.setAttribute("afno","${bkplDetails.Af_Scheme_Details}");%>
<tr><td><a href="ModifyDetails.jsp">Modify Data</a></td></tr>
<tr><td><a href="DeleteRecord">Delete Record </a></td></tr>
</table>
</body>
</html>


and my servlet is this:


import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
*
* @author Priya
*/
public class DeleteRecord extends HttpServlet {

String Af_Scheme_Number, Af_Scheme_Type, GrantedBy, Af_Scheme_Details, Amount, Approval_Date,
Completion_Period, Original_Completion_Date, Remarks,UnitName;
String query,queryDateFormat;
Connection conn;
Statement stmt;
ResultSet res;
DatabaseConnection dbconn;

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
dbconn = new DatabaseConnection();
Af_Scheme_Number = request.getParameter("afno");
conn = dbconn.setConnection();
stmt = conn.createStatement();
System.out.println("afno:"+Af_Scheme_Number);
query="Delete from bkpl where Af_Scheme_Number='"+Af_Scheme_Number+"';";
stmt.executeUpdate(query);

}catch(Exception e){
System.out.println(e.getMessage());
}
finally {

RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
rd.forward(request, response);
out.close();
}
}


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


@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}


@Override
public String getServletInfo() {
return "Short description";
}
}


But the Af number is not getting dispatched from the JSP page. Can anyone tell me how to go about it?
Carles Gasques
Ranch Hand

Joined: Apr 19, 2013
Posts: 199
    
    1
Hi Priya,

Try to replace in your servlet code

for
since you placed the "afno" value in the jsp request as an attribute


Best regards,

Priya Prasad
Ranch Hand

Joined: Jun 11, 2013
Posts: 31
Carles Gasques wrote:Hi Priya,

Try to replace in your servlet code

for
since you placed the "afno" value in the jsp request as an attribute


Best regards,




Yea I tried doing that. I am having to cast it to String after changing it to getAttribute. But it's still not working. The println shows null. Also I tried this.

In the jsp page i added this.

<tr>
<td><strong>Af Number: </strong></td>
<td><span style="font-size:smaller; font-style:italic;" name="afno">${bkplDetails.Af_Scheme_Number}</span></td>
</tr>


and then removed the <%---%> part of the jsp . It still doesnt work.
Carles Gasques
Ranch Hand

Joined: Apr 19, 2013
Posts: 199
    
    1
Hi Priya,

What do you get when iterating the query result?


Cheers,

Priya Prasad
Ranch Hand

Joined: Jun 11, 2013
Posts: 31
Carles Gasques wrote:Hi Priya,

What do you get when iterating the query result?





shows nothing.

Cheers,

Carles Gasques
Ranch Hand

Joined: Apr 19, 2013
Posts: 199
    
    1
Well then keep tracking back :-)

The problem could be the select

try to remove the question mark


Cheers,

Priya Prasad
Ranch Hand

Joined: Jun 11, 2013
Posts: 31
Carles Gasques wrote:Well then keep tracking back :-)

The problem could be the select

try to remove the question mark


Cheers,




Well that's not where the problem is. I cant get my afno passed to the servlet. the rest of the code is working fine. And the trace you wanted to do, its in the code in a different piece of code placed elsewhere and it works all fine. I can't pass the value of afno to servlet. Try running it in your IDE.
Balaji Vankadaru
Ranch Hand

Joined: May 31, 2013
Posts: 47
Please check your query line once
Priya Prasad
Ranch Hand

Joined: Jun 11, 2013
Posts: 31
Balaji Vankadaru wrote:Please check your query line once



Query line works fine.
 
Don't get me started about those stupid light bulbs.
 
subject: Passing a variable from JSP to Servlet
 
Similar Threads
Servlet throws Unload Exception
jsp and servlet inserting into database
Date Foramt via a jsp ppage through a servlet to Mysql Database
SQL syntax error
How to call Servlet using AJAX