How to get request parameter using jquery?

Hi there

I want to get request objects in my web page using jQuery

I am using servlet as backend

this is my query that I have try so far

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
 <%@ include file="/jsp/include.jsp" %>
 <%@page import="formdemo.model.Address" %>
 <%@page import="formdemo.model.HighSchool" %>
 <%@page import="formdemo.model.InterSchool" %>
 <%@page import="formdemo.model.Mobile" %>
 <%@page import="formdemo.model.PastEmployment" %>
 <%@page import="formdemo.model.Person" %>
<!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=ISO-8859-1">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Search </title>
<link href="<c:url value="/resources/css/jquery-ui.css" />" rel="stylesheet">
<link href="<c:url value="/resources/css/mainfile.css" />" rel="stylesheet">
<script src="<c:url value="/resources/js/jquery.js" />"></script>
<script src="<c:url value="/resources/js/jquery-ui.js" />"></script>
<script src="<c:url value="/resources/js/jquery.validate.js" />"></script>
<script src="<c:url value="/resources/js/registration.js" />"></script>
<script type="text/javascript">
$(document).ready(function(){
function getUrlParameter(sParam)
{
    var sPageURL = window.location.search.substring(1);
    var sURLVariables = sPageURL.split('&');
    for (var i = 0; i < sURLVariables.length; i++) 
    {
        var sParameterName = sURLVariables[i].split('=');
        if (sParameterName[0] == sParam) 
        {
            return sParameterName[1];
        }
    }
} 
var c=getUrlParameter("username");
$("#userId").val(c);
});
</script>
</head>
<body>
<div id="page-wrap">
<ul class="dropdown">
<li><a href="http://localhost:8080/WebFormSpringHibernate/jsp/registration.jsp">Add Employee</a></li>
<li><a href="http://localhost:8080/WebFormSpringHibernate/jsp/searchemployee.jsp">Search Employee</a></li>
<li><a href="http://localhost:8080/WebFormSpringHibernate/jsp/deleteemployee.jsp">Delete Employee</a></li>
<li><a href="http://localhost:8080/WebFormSpringHibernate/jsp/updateemployee.jsp">Update Employee</a></li>
</ul>
</div>
<div id="main_container">
<br>
<br>
<form action="http://localhost:8080/WebFormSpringHibernate/search" method="get">
<label style="font-size: 20px; margin-left: 40px;">Id</label><input type="text" id="userId" name="userId"/><br>
<input type="submit" value="Submit"/>
</form>
<div id="main_block"></div>
 </div>
</body>
</html>

and this is my server side script code

    super.init(getServletConfig());
        Person person=new Person();
        RestWebController controller=new RestWebController();
        try{
        person=controller.getEmployee(request.getParameter("userId"),request);
        }
        catch(Exception e){
            e.printStackTrace();
        }
            response.setContentType("plain");
            request.setAttribute("person", person);
            request.setAttribute("username", "kishor");
            request.getRequestDispatcher("/jsp/searchemployee.jsp").forward(request, response);
        System.out.println("Exiting doget() in searchEmployee");
        }

But I am not getting parameter values using jquery

EDIT
This post has been reformatted by enclosing the code block in 3 backticks
```
on their own lines.

If you want to read GET parameters from URL with javascript you can use solution from here

But much better way is to embed required values into page. The algorithm is:

  1. Get parameters from request in JSP

  2. Print them in HTML of rendered page as global variables:

and then use them in javascript (jquery or whatever) on this page

@megazoid:

This is awesome man
Many Many thanks from last 2 days I have spent more than 15 hours in Internet what I am trying to acheive and it is just 2 line of code
Thanks

here is my Updated code

$(document).ready(function(){

var param1=‘${person}’;
var param2=‘${username}’;
$(‘#userId’).val(param1);
});

Now my next question What I am trying to do is in this case “param” is a class Object which will contain some data in it.

I want to iterate that data and based on that I want to add some labels(dynamically generated) and set Text on them.

How to do that?

you can embed your variables as javascript object:

var params = {label1: '${person}', label2: '${username}'};

(where label1, label2 etc is IDs of your labels on the page)

and then iterate it in javascript:

for (labelId in params){
    if (!params.hasOwnProperty(labelId){ continue; }
    $('#'+labelId).val(params[labelId]);
}

@megazoid

ok i have did something like this

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ include file="/jsp/include.jsp" %>
<%@page import="formdemo.model.Address" %>
<%@page import="formdemo.model.HighSchool" %>
<%@page import="formdemo.model.InterSchool" %>
<%@page import="formdemo.model.Mobile" %>
<%@page import="formdemo.model.PastEmployment" %>
<%@page import="formdemo.model.Person" %>
<!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=ISO-8859-1">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Search </title>
<link href="<c:url value="/resources/css/jquery-ui.css" />" rel="stylesheet">
<link href="<c:url value="/resources/css/mainfile.css" />" rel="stylesheet">
<script src="<c:url value="/resources/js/jquery.js" />"></script>
<script src="<c:url value="/resources/js/jquery-ui.js" />"></script>
<script src="<c:url value="/resources/js/jquery.validate.js" />"></script>
<script src="<c:url value="/resources/js/registration.js" />"></script>
<script type="text/javascript">
$(document).ready(function(){
var id='${person.id}';
var firstname='${person.firstname}';
var lastname='${person.lastname}';
("#userlastname").val(lastname);
var dob='${person.dob}';
var joindate='${person.joindate}';
var email='${person.email}';
var params={"#userid":id,"#userfirstname":firstname,"#userlastname":lastname,
"#userdob":dob,"#userjoindate":joindate,"#useremail":email};
for (labelId in params){
if (!params.hasOwnProperty(labelId){ continue; }
$('#'+labelId).val(params[labelId]);
}
});
</script>
</head>
<body>
 <div id="page-wrap">
  <ul class="dropdown">
  <li><a href="http://localhost:8080/WebFormSpringHibernate/jsp/registration.jsp">Add Employee</a></li>
  <li><a href="http://localhost:8080/WebFormSpringHibernate/jsp/searchemployee.jsp">Search Employee</a></li>
  <li><a href="http://localhost:8080/WebFormSpringHibernate/jsp/deleteemployee.jsp">Delete Employee</a></li>
  <li><a href="http://localhost:8080/WebFormSpringHibernate/jsp/updateemployee.jsp">Update Employee</a></li>
  </ul>
  </div>
  <div id="main_container">
   <br>
   <br>
   <form action="http://localhost:8080/WebFormSpringHibernate/search" method="get">
   <label style="font-size: 20px; margin-left: 40px;">Id</label><input type="text" id="userId" name="userId"/><br>
   First_Name<input type="text" style="margin-left: 53px;" name="userfirstname" id="userfirstname"><br>
   Last_Name<input type="text" style="margin-left: 53px;" name="userlastname" id="userlastname"><br>
   DOB<input type="text" style="margin-left: 92px;" name="userdob" id="userdob"><br>
  Joining Date<input type="text" style="margin-left: 55px;" name="userjoindate" id="userjoindate"/><br>
  Email<input type="text" style="margin-left: 93px;" name="useremail" id="useremail"/><br>
 <input type="submit" value="Submit"/>
 </form>
 <div id="main_block"></div>
 </div>
</body>
</html>

but its still not working

this is code of my server side script

Person person=new Person();
    RestWebController controller=new RestWebController();
    try{
    person=controller.getEmployee(request.getParameter("userId"),request);
    }
    catch(Exception e){
        e.printStackTrace();
    }
        response.setContentType("plain");
        request.setAttribute("person", person);
        request.setAttribute("username", "kishor");
        request.getRequestDispatcher("/jsp/searchemployee.jsp").forward(request, response);
    System.out.println("Exiting doget() in searchEmployee");
    }

where person is a model class

Your code is messed up
upload it to pastebin.com and drop here the link, if it’s hard for you to format code here

You should not add #-signs here:

because you’re adding them here:

@megazoid

Thanks silly mistake its working know
What I want

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.