Java - Transer 2 attributes from JSP table to Servlet - java

I created a table with JSP with an image tag used to select 1 of the items. Now I want to forward two variables f.e. name and lastName.
I know how to forward 1 variable with the servlet Get method like this;
'servlet.do?name='+name But how do I obtain this variable in my Servlet and how can I send two or more attributes?

You can pass N-number of attribute while submit likewise,
'servlet.do?name='+name+'&SecondAttr='+varValue+'&ThirdAttr='+varValue
On Other Hand on Servlet you can get all Requested Parameter likewise,
Servlet.java
into doGet/doPost Method,
String name = request.getParameter("name");
String SecondAttr= request.getParameter("SecondAttr");
String ThirdAttr= request.getParameter("ThirdAttr");
Then After do whatever you want.

Related

How to retrieve an integer URL parameter with JSP? [duplicate]

In JSP how do I get parameters from the URL?
For example I have a URL www.somesite.com/Transaction_List.jsp?accountID=5
I want to get the 5.
Is there a request.getAttribute( "accountID" ) like there is for sessions or something similar?
About the Implicit Objects of the Unified Expression Language, the Java EE 5 Tutorial writes:
Implicit Objects
The JSP expression language defines a set of implicit objects:
pageContext: The context for the JSP page. Provides access to various objects including:
servletContext: The context for the JSP page’s servlet and any web components contained in the same application. See Accessing the Web Context.
session: The session object for the client. See Maintaining Client State.
request: The request triggering the execution of the JSP page. See Getting Information from Requests.
response: The response returned by the JSP page. See Constructing Responses.
In addition, several implicit objects are available that allow easy access to the following objects:
param: Maps a request parameter name to a single value
paramValues: Maps a request parameter name to an array of values
header: Maps a request header name to a single value
headerValues: Maps a request header name to an array of values
cookie: Maps a cookie name to a single cookie
initParam: Maps a context initialization parameter name to a single value
Finally, there are objects that allow access to the various scoped variables described in Using Scope Objects.
pageScope: Maps page-scoped variable names to their values
requestScope: Maps request-scoped variable names to their values
sessionScope: Maps session-scoped variable names to their values
applicationScope: Maps application-scoped variable names to their values
The interesting parts are in bold :)
So, to answer your question, you should be able to access it like this (using EL):
${param.accountID}
Or, using JSP Scriptlets (not recommended):
<%
String accountId = request.getParameter("accountID");
%>
In a GET request, the request parameters are taken from the query string (the data following the question mark on the URL). For example, the URL http://hostname.com?p1=v1&p2=v2 contains two request parameters - - p1 and p2. In a POST request, the request parameters are taken from both query string and the posted data which is encoded in the body of the request.
This example demonstrates how to include the value of a request parameter in the generated output:
Hello <b><%= request.getParameter("name") %></b>!
If the page was accessed with the URL:
http://hostname.com/mywebapp/mypage.jsp?name=John+Smith
the resulting output would be:
Hello <b>John Smith</b>!
If name is not specified on the query string, the output would be:
Hello <b>null</b>!
This example uses the value of a query parameter in a scriptlet:
<%
if (request.getParameter("name") == null) {
out.println("Please enter your name.");
} else {
out.println("Hello <b>"+request. getParameter("name")+"</b>!");
}
%>
Use EL (JSP Expression Language):
${param.accountID}
If I may add a comment here...
<c:out value="${param.accountID}"></c:out>
doesn't work for me (it prints a 0).
Instead, this works:
<c:out value="${param['accountID']}"></c:out>
request.getParameter("accountID") is what you're looking for. This is part of the Java Servlet API. See http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletRequest.html for more information.
String accountID = request.getParameter("accountID");
www.somesite.com/Transaction_List.jsp?accountID=5
For this URL there is a method call request.getParameter in java , if you want a number here cast into int, similarly for string value cast into string. so for your requirement , just copy past below line in page,
int accountId =(int)request.getParameter("accountID");
you can now call this value useing accountId in whole page.
here accountId is name of parameter you can also get more than one parameters using this, but this not work. It will only work with GET method if you hit POST request then their will be an error.
Hope this is helpful.
example you wanted to delete the subject record with its subject_id
#RequestMapping(value="subject_setup/delete/{subjectid}",method = RequestMethod.GET)
public ModelAndView delete(#PathVariable int subjectid) {
subjectsDao.delete(subjectid);
return new ModelAndView("redirect:/subject_setup");
}
and the parameter will be used for input on your query
public int delete(int subjectid) {
String sql = "update tbl_subject set isdeleted= '1' where id = "+subjectid+"";
return template.update(sql);
}
page 1 :
Detail
page 2 :
<% String id = request.getParameter("userid");%>
// now you can using id for sql query of hsql detail product

how to pass value to VO which is inside map and VO

I have CompanyProfileVO in which I have declared companyProfile_addressVOMap as map of String and VO
i want to give value to the company_name which is present in side AddressIdentificationVO in the form of url encoded
How can I set value of company_name through url encoded form?
To get the value I'm using this
CompanyProfileVO.getCompanyProfile_addressVOMap().get("COMPANY").getCompany_name()
CompanyProfileVO.java
Map<String,AddressIdentificationVO> companyProfile_addressVOMap;
AddressIdentificationVO
#FormParam("company_name")
String company_name;
According to your Code you need to create two different Variable object 1 for json and 1 for XMl(URL encoded). you should pass your
Map<String,AddressIdentificationVO> companyProfile_addressVOMap;
AddressIdentificationVO inside JSONVO and call the JSON method When u hit the URL Encoded method .

How to change url in address bar through java

How can I change the url in the address bar through java code in browser(not through javascript or any front end languages)?
I have the url in the address bar like " http://www.abcde.com/home?abc=def&ghi=jkl " but I need to change that url through java code like " http://www.abcde.com/home ". I'm using jsp's for view.
Can anyone help?
If the URL with the request parameters comes from a page on which you have no control (or if it is a link) you can do the following :
use a servlet on that URL
if it finds request parameters in the query strings, it gets them and store them in session, then redirects to itself with no query string
if there is no query string, get the parameter from session, remove them from the session to avoid to reuse them by mistake and process the query
If the parameters exists in query string, because of a form using GET method, simply change the method to POST to pass the parameters in the request body instead of the query string.

Delete cookie selenium java

I understand that if I want to delete a cookie through Selenium I should do the next:
this.getDriverProvider().get().manage().deleteCookieNamed("cookie");
But, when I created this cookie I set:
Cookie cookie = new Cookie("name=cookie", "max_age=1200");
I found that if I want to delete this cookie, I have to pass name=cookie and not cookie alone. So, I don't understand how these pairs values are used then.
Please, can somebody help me?
Thanks,
Sarang
When you create a cookie, you're passing name=cookie as its name. Constructor parameters are ordered and are mapped to its corresponding attribute so you don't have to specify that the first parameter will be the cookie's name.
If you were to add a value AFTER the creation, you invoke a method that will set the value you use as the value of the key associated to the method. For example:
Cookie c = new Cookie("name", "value");
c.setVersion("cookieVersion"); //Here, the version key will have the "cookieVersion" value
c.setMaxAge(1200);
And then, when you invoke the getName() method you'll get the value associated to the key name, passed on the corresponding constructor. In your case is "name=cookie" and in my case is just "name".
If you feel like, you can check the documentation.

to pass the value retrieved from a database to the be used in other subsequent forms

I am doing a project with the help of java,servlets and tomcat apache. First i have created a form for user login(1st form), upon submitting this, we are asked to input a phone number(2nd form) . This number i have retrieved using getParameter in the third form. But again after submitting the 3rd form,i need the phone number to be displayed in the fourth form(which is generated upon submitting the 3rd form). i tried using input tag with hidden attribute , but when i passed its value as request.getParameter, its displaying request.getParameter but not the number. Hw can i do it. I am not using jsp. I want to do directly with java and servlet.
Welcome to the Java Web Development.
I think you are in need to learn of Session and Session Handling in java web applications.
In the 3rd form when you retrieve the phoneNumber store it in a session object as:
HttpSession session = request.getSession();
session.setAttribute("phone", phoneNumber); // pair of String=key and Object=value
Now in 4th form you can get the phone number as:
Object phoneNumber = request.getSession().getAttribute("phone");
session.invalidate(); //clear-up current session
Parse this phoneNumber in whatever form you want.

Categories

Resources