aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes retrieve value at servlet from javascript. 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 » Engineering » HTML, CSS and JavaScript
Bookmark "retrieve value at servlet from javascript." Watch "retrieve value at servlet from javascript." New topic
Author

retrieve value at servlet from javascript.

Sapan Shah
Greenhorn

Joined: Jan 01, 2010
Posts: 25
I want to retrieve data which is in input text at jsp page.
Onclick calls javascript function in which i will submit it to servlet.
And that input text value should be retrieve at my servlet.When i use request.getParameter in servlet it results into null

this is my jsp page


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252"/>
<title>Diamond DNA</title>
<link type="text/css" rel="stylesheet" href="css/fixhdr.css"/>
<link type="text/css" rel="stylesheet" href="css/venus.css"/>
<script src="autosuggest.js" type="text/javascript" language="javascript"></script>
</head>

<body>
<form name="f11" id="f1" action="servlet1" method="POST">
<input type="text" id="nd" size="10">
<input type="hidden" id="hidtxt" name="hidtxt">
<input type="button" value="Click" onclick="return transfer()">
</form>
</body>
</html>



this is js page


function transfer()
{
if(document.getElementById("nd").value=="" ||document.getElementById("nd").value==null)
{
alert("");
return false;
}
document.getElementById('f1').submit();
return true;
}


This is servlet page

public class Servlet1 extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=windows-1252";

public void init(ServletConfig config) throws ServletException {
super.init(config);
}

public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>Servlet1</title></head>");
out.println("<body>");
out.println("<p>The servlet has received a GET. This is the reply.</p>");
out.println("</body></html>");

out.close();
}

public void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>Servlet1</title></head>");
out.println("<body>");
out.println("<p>The servlet has received a POST. This is the reply.</p>");
out.println("</body></html>");
String nb=request.getParameter("nd");
out.println(nb);
out.close();
}
}



David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please UseCodeTags when posting code or configuration. Unformatted code and configuration is unnecessarily difficult to read. You can edit your post by using the button. Thanks!
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Put the name on the field.

Eric
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: retrieve value at servlet from javascript.