Retrieve data from JSP - java

When I click on a link in jspA, it will redirect to jspB with query string src. The message for src will be displayed in jspB without problem. However, why I tried to click on the submit, I am unable to retrieve the value of src in my servlet page. Is there a way for me to retrieve the value of src in servlet? Thanks.
Inside my jspB page:
<img src="<%= request.getParameter("src") %>" />
<table>
<form name="frmTest" action="test" method="post">
<input type="submit" value="sub" name="sub" />
</form>
</table>
Inside my Servlet test:
#Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException{
String imgUrl = req.getParameter("src");
I am retrieving null value from the imrUrl.

When you submit an html form, only the input and select elements are sent as parameters. You don't have any that have a name attribute set to src.
You can use a hidden input
<form name="frmTest" action="test" method="post">
<input type="submit" value="sub" name="sub" />
<input type="hidden" name="src" value="<%= request.getParameter("src") %>" />
</form>
It is generally discouraged to use scriptlets. Read up on JSTL and EL and use those technologies instead.

I'm assuming you mean Submit from jspB? If so, you need to store the src value in a hidden field in the form so it is available when the servlet is called on submit. Something like the following
<form name="frmTest" action="test" method="post">
<input type="hidden" value="<%= request.getParameter("src") %>" name="src" />
<input type="submit" value="sub" name="sub" />
</form>
PS You should avoid using scriptlet (i.e. the code between <% and %>) and instead use jsp expression language

Related

Why this html button sent get request?

this code sent get request, why?
<form th:action="#{/books/edit/rename}" th:object="${book}" th:method="POST">
<input type="text" id="id" name="id" th:value="*{id}" value="1"/>
<input type="text" id="bookName" name="bookName" th:value="*{bookName}" value="nameExample"/>
<button type="submit">Rename for model</button>
</form>
Try removing the th: in the method declaration, to be as the following:
<form th:action="#{/books/edit/rename}" th:object="${book}" method="POST">
…
Because it is not reading the code and the default is GET, check this for more details https://www.w3.org/TR/html401/interact/forms.html#h-17.3

No action attribute in html form for Jsoup login

I'm trying to login a website (vimla.se) using Jsoup in android. I'm aware that when submitting forms in html, action is the attribute which we use to POST the login credentials using Jsoup (as explained here). However, in my case, there's no action pointer and the html form looks something like this:
<form id="loginForm" name="loginForm" ng-submit="login()" method="POST">
<input type="email" id="username" form-filler required="required" class="text txtEmail" name="username" placeholder="E-mail" autofocus="autofocus" ng-model="username" />
<br />
<input type="password" id="password" required="required" class="text txtPass" name="password" form-filler placeholder="Password" autofocus="autofocus" ng-model="password" />
<br />
<button type="submit" class="btn" ng-disabled="sending">Login</button>
</form>
So my question is, how do we login such forms using Jsoup?
This is actually a form using Angular.js. The action - attribute is not specified, but ng-submit https://docs.angularjs.org/api/ng/directive/ngSubmit
describes what to do on submit. The LoginController then implements the function that gets executed. Its implementation is hidden in https://vimla.se/scripts/all.min.js?v=1.0.0.0.
a.login=function(){a.sending||(a.sending=!0,a.error=!1,b.post("/user/login",{username:a.username,password:a.password,referer:a.referer})
So the url that gets called is /user/login and the parameters transmitted are username, password, referer

on click the button value should get in jsp page is null

HTML Page
<form action="viewParticularImage.jsp" method="post">
input type="submit" name="1" value="Details" />
input type="submit" name="2" value="Details" />
input type="submit" name="3" value="Details" />
</from>
JSP page
<%
String name = request.getParameter("name");
out.write(name);
%>
i am getting null value in name while printing the button Details in JSP Page pls help to find the solution, thanks to the replies in advance .
You have parameters named as "1", "2" and "3", but you have not parameter named as "name".
<form action="viewParticularImage.jsp" method="post">
<input type="submit" name="name" value="Details" />
...
</from>
In your JSP page make it
<%
String name = request.getParameter("1");
out.write(name);
%>
You are not sending a parameter named name in the request.That's why! To confirm, try
<%
String name = request.getParameter("1");
out.write(name);
%>
Your form should be like below. See the value of name attribute of the input text. The name attribute denotes the name of the request parameter
<form action="viewParticularImage.jsp" method="post">
First name: <input type="text" name="name"><br>
<input type="submit" value="Submit">
</form>
Note : Try entering a value in the text and submit. You will receive it in the server request
I believe you haven't gained any knowledge regarding HTML ... First, go and check the tutorials regarding HTML...
Let's get to the game ... What are you trying to do ... Are you trying to pull down values set to the submit button ... or you are trying to publish a textfield where user can type their name and show that name as the result in the next page ????
If you are trying to publish the values assigned to the submit button...
HTML Page
<form action="viewParticularImage.jsp" method="post">
<input type="submit" name="1" value="Details" />
<input type="submit" name="2" value="Details" />
<input type="submit" name="3" value="Details" />
</form>
JSP page
<%
String name = request.getParameter("1");
out.write(name);
%>
If you are trying to publish what users type ...
HTML Page
<form action="viewParticularImage.jsp" method="post">
<input type="text" name="name" />
<input type="submit" value="Hit Send" />
</form>
JSP page
<%
String name = request.getParameter("name");
out.write(name);
%>
I think this clarifies you how things work !!!! Thank you ... peace !!!

calling Servlet's method from JSP

I am developing a web application using JSP and Servlets.
I wants to call servlet's method from JSP page when user click's on the Update button.
<input type="button" value="Update" onclick="<%MyServletName.Update(request, response);%>"></input>
When I click on Update button then this method is calling the servlet, But the problem is that when form is loaded then this method is called automatically.
Thanks in advance.....
Source Code....
<%#page import="MyPackageName.MyServletName"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Update</title>
</head>
<body>
<form>
<%
String[][] data = (String[][])request.getAttribute("data");
String[] columnNames = (String[])request.getAttribute("columnNames");
//fill the table data
if(columnNames.length!=0 && data.length!=0)
{
%><table><%
}
for(int i=0;i<data.length;i++){
%>
<tr> <td><%= columnNames[0] %></td> <td><input type="text" name="text1" value="<%= data[i][0] %>"></td> </tr>
<tr> <td><%= columnNames[1] %></td> <td><input type="text" name="text2" value="<%= data[i][1] %>"></td> </tr>
<tr> <td><%= columnNames[2] %></td> <td><input type="text" name="text3" value="<%= data[i][2] %>"></td> </tr>
<%
}
%>
<tr>
<td></td>
<td><input type="button" value="Update" onclick="<%PlanProtocolEdit.Update(request, response);%>"></input></td>
</tr>
</table>
</form>
</body>
</html>
Is there any way that I can call the servlet method other than dogGet() and doPost() without invoking the servlets dogGet() and doPost() methods?
You should specify the action on the form itself not the actual input button.
If you define an action on the form. E.g.
The forms submit button will submit the form to that URL. E.g.
<form action="/mypath/someurl">
<input type="submit" value="Submit" />
</form>
You can add an additional attribute to the form tag to specify whether you want the request to be a get or a post.
<form action="/mypath/someurl" method="POST">
Would send a post request, you can then pick this up in your Servlet with the handlePost method
The above approach should be used, currently you are trying to call a Java method on a javascript onclick event. This is incorrect and it is not doing what you think it is.
The code in PlanProtocolEdit. Update should be in a doGet or doPost method of a servlet which will be triggered by configuring your form as described above.
<input type="button" value="Update" onclick="<%MyServletName.Update(request, response);%>"></input>
This line in your jsp will be evaluated to
<input type="button" value="Update" onclick=""></input> in HTML page. If you want to call your servlet on click, first map your servlet to a url path say /myservletpath in web.xml and then
use
<input type="button" value="Update" onclick="location.href='/myservletpath'"></input>
You can call a servlet method other than doPost() and doGet() by creating a useBean of jsp.
<jsp:useBean id="someid" class="SomePackageName.PlanProtocolEdit">
and call the servlet method onclick as:
<input type="button" value="Update" onclick="<% someid.Update(args[0], args[1]); %>" />
This jsp bean would identify your servlet class which you now can access in your jsp page with the id given to it in useBean tag.
Do not forget to close the useBean tag.

Using post method along with jquery modal window

I am developing an application using jquery ui and servlets.I have used modal dialog window for login.Once I Login the credentials are being sent to LoginServlet where the crendentials are checked and the user is being redirected to new page.
now Login.jsp has:
<html>
<head>
<script>
$(document).ready(function() {
$("#dialog").dialog();
</script>
</head>
<body style="font-size: 62.5%;">
<div id="dialog" title="DBoperations">
<form id="LoginForm" method="post" action="Login">
<fieldset>
<label>Username:</label>
<input type="text" id="username" value=""></input><br></br>
<label>Password:</label>
<input type="password" id="password" value=""></input><br></br>
<input type="submit" id="submit" class="submit" value="Log In" align="middle"></input>
</fieldset>
</form>
</div>
</body>
Now when I run the application the data passed to the servlet is null.I checked it using println statements.As far as I know al it takes to pass data to servlet is specifying action and using getparameter on server side...
I am gettin Null pointer exception due to the null value being passed to the login method..
why are null values passed??
Try adding a name attribute to your inputs and see if that works. I believe the browser only sends the request parameter if it has a name attribute.
<input type="text" id="username" name="username" value="">

Categories