<input type="checkbox" name="premium" value="HBO">HBO <br>
<input type="checkbox" name="premium" value="FOXP">FOX Película <br>
<input type="checkbox" name="premium" value="FOX">FOX + <br><br>
<input type="submit" value="cotizar" name="Cotizar" />
<br><br>
I'm trying to bring from an index.jsp the premium parameter of a checkbox, in a servlet of a client of a web service. I have to save it as a list but what I have tried has not worked
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String plan = request.getParameter("plan");
String PREMIUM = request.getParameter("premium");
How do I convert request.getParameter ("premium") into a list?
Try this:
String[] premiums = request.getParameterValues("premium");
You need to put all the checkbox inside of form then only multiple values will be sent to server.
HTML:
<form method="post">
<input type="checkbox" name="premium" value="HBO">HBO <br>
<input type="checkbox" name="premium" value="FOXP">FOX Película <br>
<input type="checkbox" name="premium" value="FOX">FOX + <br><br>
<input type="submit" value="cotizar" name="Cotizar" />
</form>
Servlet:
To get multiple values we need to use getParameterValues(), if you any value for the given parameter name it will give string array of values. If we have one value it will given string array with length of 1.
String[] premiums = request.getParameterValues("premium");
If no value sent to server then above method will return null value.
Related
I want to get the values for the rows which is then sent to a Java servlet then read by another page and inserts those values into the text boxes for the user to edit and write it back into the text file.
So it gets read by ProductIO which reads the textfile.
Its then entered into a jsp table
<c:forEach var="product" items="${products}">
<tr>
<td ><c:out value='${product.code}'/></td>
<td ><c:out value='${product.description}'/></td>
<td >${product.priceCurrencyFormat}</td>
<td><form action="editproduct" method="post">
<input type="submit" value = "Edit">
</form>
</td>
<td><form action="deleteproduct" method="post">
<input type="submit" value = "Delete">
</form>
</td>
</tr>
</c:forEach>
The User Clicks either the delete or edit button which will then send action to either the deleteproduct servlet or the editproduct servlet(only asking about the edit for now)
The edit product servlet
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
String url = "/editproduct.jsp";
getServletContext()
.getRequestDispatcher(url)
.forward(request, response);
String action = request.getParameter("action");
if (action == null) {
action = "editproduct"; // default action
} else if (action.equals("editproduct")) {
String productCode = request.getParameter("productCode");
String descString = request.getParameter("description");
//HttpSession session = request.getSession();
Product product = (Product) session.getAttribute("cart");
if (product == null) {
product = new Product();
}
getServletContext()
.getRequestDispatcher(url)
.forward(request, response);
}
}
Which the three Values are put into three text boxes on the editProduct.jsp page(where im having a problem getting the values to insert into the text boxes for it to be written back to the text file with the new information)
<form action="Product" method="post" >
<input type="hidden" name="action" value="add">
<label>Code:</label>
<input type="text" name="code" value='<%=request.getAttribute("code")%>'
required><br>
<label >Description:</label>
<input type="text" name="desc" value='<%=request.getAttribute("description")%>'
required ><br>
<label >Price:</label>
<input type="text" name="price" value='<%=request.getAttribute("price")%>'
required ><br>
<label> </label>
<!--<input type="submit" value="Update Product" class="margin_left">-->
<!--<input type="submit" value="View Product" class="margin_left">-->
<button type="submit">Update</button><br>
I can share more code if needed.
You aren't calling request.setAttribute() with any of the attributes in your Servlet. I assume you meant to add something like
request.setAttribute("code", productCode);
request.setAttribute("description", descString);
request.setAttribute("price", product.getPrice());
Before forwarding the request.
I'm trying to implement LTI in our education platform to connect with QTIWoks in Java.
I have a simple tool consumer that generates the following HTML Form:
<html>
<head> </head>
<body>
<form action="http://192.168.0.114:8080/qtiworks-engine/lti/domainlaunch" name="ltiLaunchForm" id="ltiLaunchForm" method="post" target="basicltiLaunchFrame" enctype="application/x-www-form-urlencoded" style="display: block;">
<input type="hidden" name="context_id" value="cid-00113">
<input type="hidden" name="context_label" value="SI106">
<input type="hidden" name="context_title" value="Design of Personal Environments 1">
<input type="hidden" name="ext_note" value="Instructor from first course">
<input type="hidden" name="launch_presentation_locale" value="en_us">
<input type="hidden" name="lis_person_contact_email_primary" value="sian#imscert.org">
<input type="hidden" name="lis_person_name_family" value="Instructor">
<input type="hidden" name="lis_person_name_given" value="Siân">
<input type="hidden" name="lis_person_sourcedid" value="school.edu:user">
<input type="hidden" name="resource_link_description" value="This learning space is private">
<input type="hidden" name="resource_link_id" value="res-0012612">
<input type="hidden" name="resource_link_title" value="My Weekly Wiki">
<input type="hidden" name="roles" value="Instructor">
<input type="hidden" name="tool_consumer_info_product_family_code" value="sakai-unit">
<input type="hidden" name="tool_consumer_info_version" value="0.9">
<input type="hidden" name="tool_consumer_instance_description" value="University of School (LMSng)">
<input type="hidden" name="tool_consumer_instance_guid" value="lmsng.school.edu">
<input type="hidden" name="user_id" value="user-0016">
<input type="hidden" name="oauth_callback" value="about:blank">
<input type="hidden" name="custom_simple_key" value="custom_simple_value">
<input type="hidden" name="custom_complex____________key" value="Complex!##$^*(){}[]½Value">
<input type="hidden" name="lti_version" value="LTI-1p0">
<input type="hidden" name="lti_message_type" value="basic-lti-launch-request">
<input type="hidden" name="oauth_version" value="1.0">
<input type="hidden" name="oauth_nonce" value="Z2WVNEPUZkzsolOe4hRvKbkXtOSmYiyw">
<input type="hidden" name="oauth_timestamp" value="1429793062">
<input type="hidden" name="oauth_consumer_key" value="feras">
<input type="hidden" name="oauth_signature_method" value="HMAC-SHA1">
<input type="hidden" name="oauth_signature" value="g908qTtVGh8MsOgVUdarVlSBmC0=">
<input type="hidden" name="ext_submit" value="Finish Launch">
</form><iframe name="basicltiLaunchFrame" id="basicltiLaunchFrame" src="" width="100%" height="900" scrolling="auto" frameborder="1" transparency="">
</iframe>
<script type="text/javascript">
document.getElementById("ltiLaunchForm").style.display = "none";
nei = document.createElement('input');
nei.setAttribute('type', 'hidden');
nei.setAttribute('name', 'ext_submit');
nei.setAttribute('value', 'FinishLaunch');
document.getElementById("ltiLaunchForm").appendChild(nei);
document.ltiLaunchForm.submit();
</script>
</body>
</html>
then it calls the dominlaunch from qtiworks
#RequestMapping(value="/domainlaunch", method=RequestMethod.POST)
public String ltiDomainLevelLaunch(final HttpSession httpSession, final HttpServletRequest request,
final HttpServletResponse response)
throws IOException {
/* Decode LTI launch request, and bail out on error */
final DecodedLtiLaunch decodedLtiLaunch = ltiLaunchService.decodeLtiLaunchData(request, LtiLaunchType.DOMAIN);
if (decodedLtiLaunch.isError()) {
response.sendError(decodedLtiLaunch.getErrorCode(), decodedLtiLaunch.getErrorMessage());
return null;
}
final LtiLaunchData ltiLaunchData = decodedLtiLaunch.getLtiLaunchData();
/* Make sure this is a domain launch */
final LtiUser ltiUser = decodedLtiLaunch.getLtiUser();
final LtiDomain ltiDomain = ltiUser.getLtiDomain();
if (ltiDomain==null) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST, "The tool consumer has attempted a domain-level launch using a link-level key");
return null;
}
/* Extract/create the corresponding LtiResource for this launch */
final LtiResource ltiResource = ltiLaunchService.provideLtiResource(decodedLtiLaunch); /* (May be null for candidates) */
final UserRole userRole = ltiUser.getUserRole();
if (userRole==UserRole.INSTRUCTOR) {
/* If user is an instructor, we'll forward to the LTI instructor MVC after
* "authenticating" the user by creating and storing an LtiDomainTicket
* in the session */
final LtiAuthenticationTicket ltiDomainTicket = new LtiAuthenticationTicket(ltiUser.getId(), ltiResource.getId(),ltiLaunchData.getLaunchPresentationReturnUrl());
LtiResourceAuthenticationFilter.authenticateUserForResource(httpSession, ltiDomainTicket);
return "redirect:/lti/resource/" + ltiResource.getId();
}
else if (userRole==UserRole.CANDIDATE) {
/* If user is a candidate, then we'll launch/reuse a candidate session */
if (ltiResource==null) {
return "candidateLaunchError";
}
/* Extract relevant data */
final String returnUrl = ltiLaunchData.getLaunchPresentationReturnUrl();
final String lisOutcomeServiceUrl = ltiLaunchData.getLisOutcomeServiceUrl();
final String lisResultSourcedid = ltiLaunchData.getLisResultSourcedid();
/* Launch and redirect to session */
try {
final CandidateSessionTicket candidateSessionTicket = candidateSessionLaunchService.launchDomainLevelLtiCandidateSession(httpSession,
ltiUser, ltiResource, returnUrl, lisOutcomeServiceUrl, lisResultSourcedid);
return GlobalRouter.buildSessionStartRedirect(candidateSessionTicket);
}
catch (final CandidateException e) {
return "candidateLaunchError";
}
}
else {
throw new QtiWorksLogicException("Unexpected LTI userRole " + userRole);
}
}
the problem is decodedLtiLaunch.isError() returns true and my request is not performed.
I debugged the problem and found that HttpServletRequest request has no item in parameterMap
however, it's working well when the request comes from moodle (request's parameterMap has the passed params)
how can I solve this please?
thanks in advance.
Without knowing what the exact error code you are seeing is, I am going to make a very strong guess that your signature is bad
Since you are using JAVA I would suggest that you utilize the basiclti-util library that IMSGlobal provides to generate a signature
Add the following dependency
<dependency>
<groupId>org.imsglobal</groupId>
<artifactId>basiclti-util</artifactId>
<version>1.1.2</version>
</dependency>
Then use the following code to generate the signature
Map<String, String> signedParameters = new LtiOauthSigner()
.signParameters(parameters, key, secret, url, "POST");
Then take all of the key-value pairs in the signedParameters map, and use that to construct the form input tags you have in your example.
I'm new to servlets and jsp files, and I encountered the following problem:
I have the following jsp form file:
<FORM action="http://myApp/register" method="post">
<P>
First name: <INPUT type="text" name="firstname"><BR>
Last name: <INPUT type="text" name="lastname"><BR>
email: <INPUT type="text" name="email"><BR>
<INPUT type="radio" name="sex" value="Male"> Male<BR>
<INPUT type="radio" name="sex" value="Female"> Female<BR>
<INPUT type="submit" value="Send"> <INPUT type="reset">
</P>
</FORM>
and a servlet which handles a doPost request on the url above.
I want to give the values of the form (such as the first name and last name) to the doPost request.
Any ideas?
Thanks a lot! :)
You can get all the parameters by their name as request.getParameters("firstname") to get the value of the input fields.
public class ServletClass extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String firstName = request.getParameter("firstname");
String lastName = request.getParameter("lastName");
// and so on.....
}
}
I am newbie in HTML and Servlets. I have such a idex.jsp contents:
<form action="createCSV" method="get">
<input type="text" value="D:/">
<input type="submit" value="create">
</form>
And I want to use value of <input type="text" value="D:/"> inside of method Get in my Servlet:
protected void doGet( javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response )
throws javax.servlet.ServletException, IOException{}
Please, help me
change the input field in your html form like this:
<input type="text" name="userInput" value="D:/">
inside the servlet do that:
String userInput = request.getParameter("userInput");
I have 2 textboxes and 3 buttons and each button has specific action i.e. insert, update, delete.
But how to redirect it when specific button click ?
because i have used it in one form tag.
so what will it do when any button click ?
how it will get the action name ?
code :
<form action="Doaction" method="post">
First Name : <input type="text" id="fname" name="fname">
<br>
Last Name : <input type="text" id="lname" name="lname"><br>
<input type="button" value="Insert">
<input type="button" value="Update">
<input type="button" value="Delete">
</form>
any help please ?
in form : give name to buttons
First Name : <input type="text" id="fname" name="fname">
<br>
Last Name : <input type="text" id="lname" name="lname"><br>
<input type="button" value="Insert" name="button">
<input type="button" value="Update" name="button">
<input type="button" value="Delete" name="button">
</form>
String button_param = request.getParameter("button");
RequestDispatcher rd = null;
if(button_param .equals("Insert")
{
rd=request.getRequestDispatcher("InsertServlet");
}
else if(button_param .equals("Update"))
{
rd=request.getRequestDispatcher("UpdateServlet");
}
else if(button_param .equals("Delete"))
{
rd=request.getRequestDispatcher("DeleteServlet");
}
rd.forward(request, response);
you can create a common javascript function like submitForm('pass your action').
Here you can see that function has a parameter. you have to pass action based your requirement
for example :
<input type="button" value="Insert" onclick="submitForm('InsertURL');">
<input type="button" value="Update" onclick="submitForm('UpdateURL');">
Action function body like :
function submitForm(action){
//send Ajax Request to server...using action
}
Yes I have solved it.
I have given a common name to each button and in servlet i have first checked that from which button action call then as per it i have used if condition and inside it's block i have make a related code.
code :
<form action="Doreg" method="post">
First Name : <input type="text" id="fname" name="fname">
<br>
Last Name : <input type="text" id="lname" name="lname"><br>
<input type="submit" value="Insert" name="kb">
<input type="submit" value="Update" name="kb">
<input type="submit" value="Delete" name="kb">
</form>
java code :
String fname =request.getParameter("fname");
String lname =request.getParameter("lname");
String f = request.getParameter("kb");
if (f.equals("Insert"))
{
obj.connect();
String query ="insert into user(firstname,lastname) values('"+fname+"','"+lname+"')";
obj.passquery(query);
}
else if(f.equals("Delete"))
{
obj.connect();
String query ="delete user where firstname='"+fname+"' OR lastname='"+lname+"'";
obj.passquery(query);
}