• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Need help on Passing parameters

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}

}

 
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the servlet, use request.getParameter() to fetch the submitted values of the form controls.
 
Bear Bibeault
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And, generating HTML in a servlet: bad practice. Java code in a JSP: bad practice.

 
Sam Saha
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please show me, in my reply, where I recommended using request.getAttribute().
 
Sam Saha
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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);
}


}

 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would recommend that you look up the difference between request.getParameter and request.getAttribute.

They are very different beasts.
 
Sam Saha
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure. Thank you for your good suggestion and I will obey you. Thank you one more time.
 
Sam Saha
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Once again -- attention to detail. Don't assume that it is working, prove it!
 
Sam Saha
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the result in the JSP in the attachment.

form.PNG
[Thumbnail for form.PNG]
The user name display in the JSP
 
Bear Bibeault
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic