aspose file tools*
The moose likes JSP and the fly likes Need help on Passing parameters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Need help on Passing parameters" Watch "Need help on Passing parameters" New topic
Author

Need help on Passing parameters

Sam Saha
Ranch Hand

Joined: Jan 23, 2010
Posts: 104
I have a form where I am entering the message in the message box and the username is generating as read only. By post method I am calling a servlet. Now I wanted to pass the values of the what I have typed in the message box as well as the user name from the JSP to the servlet. Please help me how I can pass them to servlet.


The form in the JSP:



<HEAD>
<TITLE>Thank you for your feedback!</TITLE>


<div id="nav">

<%
String feedbackStr = "";
feedbackStr = "../ame/feedback";

%>

<form name="jsform" action="<%= feedbackStr%>" method="post">

<table><tr>
<td align="right">Name:</td>
<td><p id="username"></p></td>
</tr>
<tr>
<td align="right"><input type="checkbox" name="feedback" value="fb" /></td>
<td>Send a copy to the user</td>
</tr>
<tr>
<td colspan="2">
Your message:<br>
<textarea name="message" cols="31" rows="6" wrap="soft">
</textarea>
</td>
</tr>
<tr>
<td colspan="2">
<center>
<input type="submit" value="Send Message">
</center>
</td>
</tr></table>
</form>


</div>
</HEAD>




The Servlet code:

public class feedback extends HttpServlet {

private UserFeedbackWSCall feedbackWebServiceCall = new UserFeedbackWSCall();

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {

try{
response.setContentType("text/html");
java.io.PrintWriter out = response.getWriter();
out.println("<html><head>");
out.println("<title>Feedback Submission Connfirmation</title></head><body>");
out.println("<h2>Thank you for your Feedback!</h2>");
out.println("</body></html>");

}catch (Exception e){
throw new ServletException(e.getMessage());
}

} //doGet

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
doGet(request, response);
}

}
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61419
    
  67

In the servlet, use request.getParameter() to fetch the submitted values of the form controls.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61419
    
  67

And, generating HTML in a servlet: bad practice. Java code in a JSP: bad practice.

Sam Saha
Ranch Hand

Joined: Jan 23, 2010
Posts: 104
I am using the code out.println("Message"+request.getAttribute("message")); in the sevlet but I am getting the null value. Please help me. Theanks.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61419
    
  67

Please show me, in my reply, where I recommended using request.getAttribute().
Sam Saha
Ranch Hand

Joined: Jan 23, 2010
Posts: 104
public class feedback extends HttpServlet {

private UserFeedbackWSCall feedbackWebServiceCall = new UserFeedbackWSCall();

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {

String applicationCD = "";
String requestedByLanId = "";
UserFeedback feedback = new UserFeedback();

System.out.print("I have successfully Entered into the feedback sevlet"+request.getAttribute("message"));

try{
response.setContentType("text/html");
java.io.PrintWriter out = response.getWriter();

out.println("<html><head>");
out.println("<title>Feedback Submission Connfirmation</title></head><body>");
out.println("<h2>Thank you for your Feedback!</h2>");
out.println("Message"+request.getAttribute("message"));
out.println("</body></html>");

}catch (Exception e){

throw new ServletException(e.getMessage());

}

} //doGet

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
doGet(request, response);
}


}
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
I would recommend that you look up the difference between request.getParameter and request.getAttribute.

They are very different beasts.
Sam Saha
Ranch Hand

Joined: Jan 23, 2010
Posts: 104
I am extremely sorry. I am very new in web development. I correct my code and used request.getParameter("message") instead of request.getAttribute("message"). I am geeting the value in the servlet. Thank you very much.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61419
    
  67

Programming is all about attention to detail. In my early reply I told you to use getParameter(), not getAttribute(). Paying attention to details like this is mandatory if you are to make progress.
Sam Saha
Ranch Hand

Joined: Jan 23, 2010
Posts: 104
Sure. Thank you for your good suggestion and I will obey you. Thank you one more time.
Sam Saha
Ranch Hand

Joined: Jan 23, 2010
Posts: 104
I am trying pass the user name value to the servlet from the JSP using out.println("User Name: " + request.getParameter("fdbackurname")); in the servlet. But the user name is showing null. Please help me.

Here is JSP code:

<td align="right">Name:</td>
<td><p id="username" name="fdbackurname"></p></td>
</tr>


User name is displaying in the JSP by the following sript:

<script type='text/javascript' src="scripts/jquery.autocomplete.js"></script>
<script language="JavaScript"><!--

var XMLHttpRequestObjectRVT = false;
var XMLHttpRequestObjectUDN = false;
if (window.XMLHttpRequest) {
XMLHttpRequestObjectRVT = new XMLHttpRequest();
XMLHttpRequestObjectUDN = new XMLHttpRequest();
} else if (window.ActiveXObject) {
XMLHttpRequestObjectRVT = new ActiveXObject("Microsoft.XMLHTTP");
XMLHttpRequestObjectUDN = new ActiveXObject("Microsoft.XMLHTTP");
}
var mccn_viewer = "<%=((String)request.getSession().getAttribute("MCCN_VIEWER"))%>";

function callUserDisplayName(){
var dataSource="userDisplayName";
var obj2 = document.getElementById("username");

var obj3 = document.getElementById("usernameFB");

var str = "";

if(XMLHttpRequestObjectUDN) {
XMLHttpRequestObjectUDN.open("Post", dataSource);
XMLHttpRequestObjectUDN.onreadystatechange = function() {
if (XMLHttpRequestObjectUDN.readyState == 4 &&
XMLHttpRequestObjectUDN.status == 200) {
var responseXML2 = XMLHttpRequestObjectUDN.responseXML;
if (responseXML2 != null ){
var root = responseXML2.documentElement;
var displayName = root.getElementsByTagName("DisplayName")[0].text;
if(displayName != null && displayName != "" && displayName !="Guest"){
obj2.innerHTML = "<Strong>"+displayName+"</Strong>";
if(obj3 != ""){
obj3.innerHTML = "<h5>Name: <strong>"+displayName+"</strong></h5>";
}
}else {
obj2.innerHTML = "<Strong>"+"Guest"+"</Strong>";
if(obj3 != ""){
obj3.innerHTML = "<h5>Name: <strong>"+"Guest"+"</strong></h5><h6>For a direct response, please provide your name in the feedback area below.</h6>";
}
}

}else {
obj2.innerHTML = "Welcome " +"<Strong>"+"Guest"+"</Strong>";
if(obj3 != ""){
obj3.innerHTML = "<h5>Name: <strong>"+"Guest"+"</strong></h5><h6>For a direct response, please provide your name in the feedback area below.</h6>";
}
}
}

}
XMLHttpRequestObjectUDN.send(null);
}

}

WindowOnload(callUserDisplayName);

function WindowOnload(f) {
var prev=window.onload;
window.onload=function(){ if(prev)prev(); f(); }
}


--></script>
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61419
    
  67

Hmmm, I'm trying to look at your code, but the stubborn forum software has made it look all wonky. If you use CODE tags when posting code to the forums (see the FAQ at ⇒ UseCodeTags ⇐) they tell it "Hey! Keep your hands off my formatting!" and your code will look just like when you cut and pasted it into the reply. Cool, no?

The best way to debug this is to use a tool like Firebug in Firefox (or the builtin tools for IE8, Safari or Chrome) to actually look at what is being passed in the HTTP request. That will tell you whether the issued is the client code not sending the info correctly, or the server code not fetching it correctly (or perhaps a little of both).
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61419
    
  67

And.... if it turns out that your Ajax code is the issue, do yourself a favor and adopt one of the modern JavaScript libraries (such as jQuery or Dojo) to do your Ajax for you. No self-respecting seasoned developer writes their own Ajax code anymore.
Sam Saha
Ranch Hand

Joined: Jan 23, 2010
Posts: 104
I think the AJAX script is working fine. Because I am getting the User name in the JSP page. But when I am trying to get the user name in the servlet I am getting the user name is null in the servlet.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61419
    
  67

Once again -- attention to detail. Don't assume that it is working, prove it!
Sam Saha
Ranch Hand

Joined: Jan 23, 2010
Posts: 104
Here is the result in the JSP in the attachment.




[Thumbnail for form.PNG]

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61419
    
  67

I am not interested in downloading images or seeing what the end result is.

The question remains: are the parameters as you expect them to be, passed correctly in the HTTP request? Looking at the HTTP request itself, not the end result of a JSP, is the fastest and best way to diagnose such issues.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help on Passing parameters