How to retrieve text box value from a JSP page [duplicate] - java

This question already has answers here:
How can I upload files to a server using JSP/Servlet?
(14 answers)
Closed 7 years ago.
I am creating a web application in Java and am having trouble retrieving a value from a text box. My aim is to ask the user to enter their email address and then use the value entered for the rest of my application. I do this by attempting to pass the value as part of the URL in the jsp file (and retrieve it using request.getParameter()).
However, the value that I keep retrieving is null.
Here is my code:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>
<TITLE>Display file upload form to the user</TITLE>
</HEAD>
<center>
<label for="email">Enter email address</label>
<input id="email" name="email">
</center>
<% String mail = request.getParameter("email");
System.out.println(mail);%>
<BODY>
<FORM ENCTYPE="multipart/form-data" ACTION="upload.jsp?e=<%=mail%>" METHOD=POST>
<br> <br> <br>
<center>
<table border="0" bgcolor=#ccFDDEE>
<tr>
<center>
<td colspan="2" align="center"><B>UPLOAD THE FILE</B>
<center></td>
</tr>
<tr>
<td colspan="2" align="center"></td>
</tr>
<tr>
<td><b>Choose the WebEx File To Upload and Convert:</b></td>
<td><INPUT NAME="file" TYPE="file"></td>
</tr>
<tr>
<td colspan="2" align="center"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit"
value="Upload and Convert Recording"></td>
</tr>
<table>
</center>
</FORM>
</BODY>
</HTML>
I am trying to pass the mail value into the jsp file so that I can use it in my application

Put the following code between form tag.
<form ENCTYPE="multipart/form-data" ACTION="upload.jsp" METHOD=POST>
<center>
<label for="email">Enter email address</label>
<input id="email" name="email">
</center>
-------
-------
</form>
on Server side write
String email=request.getParameter("email");
this will definitely solved your problem.

you are requesting for upload.jsp?e=<%=mail%> where the parameter name is e and an other side you are getting the request.getParameter("email");
try this request.getParameter("e");or use this instead upload.jsp?email=<%=mail%> for request.getParameter("email");

you can try :
<form .... ACTION="upload.jsp?e=${mail}" method="POST">
And send parameters :
request.setAttribute("mail", "mymail#abc.com");

As per my understanding you are sending both parameters and file to the server by specifying multipart/form-data as form encryption type. As per my knowledge in this mode, normal parameters except files are not available directly on server side.
If you can use third party libraries in your application, you can use Apche Commons and Commons IO libraries. You can also use the example specified here commons file uploading to overcome your problem.
You can visit multipart/form-data structure for more details.

Related

How can I submit a html form post that has several different iterated values?

This is a Spring MVC project.
So the following JSP
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<table>
<c:forEach items="${nameList}" var="studentList">
<tr>
<td><c:out value="${studentList.name}"/> ${studentList.regNo}</td>
</tr>
</c:forEach>
</table>
returns values like
Ramu
Somu
Mamu
Komu
I want to make each value as a post url, so that, if the user clicks any one link, I would like to submit as like the following jsp code does
<form method="post" action="number" id="number">
<div style="text-align: center;" >
<input width="20" type="text" data-validation="numbers" id="regNo" name="regNo" size="30" maxLength="50" placeholder="Enter Register Number">
</div>
</form>
I don't wanna do GET.
How can I do this? Please help me.
Use this
<td><a href='www.xyz.com:port/number?regNo=${studentList.regNo}><c:out value="${studentList.name}"/> </a></td>
And regNo you can get as the request parameter in controller
or
Path parameter in your controller like
<td><a href='www.xyz.com:port/number/${studentList.regNo}><c:out value="${studentList.name}"/> </a></td>
And modify your controller's configuration accordingly.

struts resource bundle properties file not mapping some keys

I'm starting to study struts and I have a problem using resource properties file
some text on page is displayed as this:
???login.message???
???login.username???
???login.password???
but some other messages are correctly taken from properties file. I think that the propertis file is correctly configured but I'm missing something to display anything correctly.
the file ApplicationResources.properties
# Resources for Login Project
# Struts Validator Error Messages
# These two resources are used by Struts HTML tag library
# to format messages. In this case we make sure that errors
# are red so that they can be noticed.
errors.header=<font color="red">*
errors.footer=</font>
#errors associated with the Login page
error.username.required=username required.
error.password.required=password required
error.login.invalid=The system could not verify your username or password. Is your CAPS LOCK on? Please try again.
#login page text
login.title=this is a title
login.message=please log in
login.username=username:
login.password=password:
login.button.signon=Log In
#loggedin page text
loggedin.title=Login Project
loggedin.msg=Benvenuto, {0}. You are now logged in.
"error.login.invalid" is correctly displayed and "error.username.required" too
the login label not
this is my jsp page
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%# taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt" %>
<%# taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<html:html locale="true"/>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<fmt:bundle basename="ApplicationResources"/>
<title><fmt:message key="login.title"/></title>
</head>
<body>
<html:errors property="login"/>
<html:form action="login.do" focus="userName" >
<table align="center">
<tr align="center">
<td><H1><fmt:message key="login.message"/></H1></td>
</tr>
<tr align="center">
<td>
<table align="center">
<tr>
<td align="right">
<fmt:message key="login.username"/>
</td>
<td align="left">
<html:text property="userName"
size="15"
maxlength="15" />
<html:errors property="userName" />
</td>
</tr>
<tr>
<td align="right">
<fmt:message key="login.password"/>
</td>
<td align="left">
<html:password property="password"
size="15"
maxlength="15"
redisplay="false"/>
<html:errors property="password" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<html:submit>
<fmt:message key="login.button.signon"/>
</html:submit>
</td>
</tr>
</table>
</td>
</tr>
</table>
</html:form>
</body>
</html>
Can you help me ?
tkz
Your
<fmt:message ... />
tags need to be inside an
<fmt:bundle ... >
tag. Currently you are closing your bundle tag right away
<fmt:bundle basename="ApplicationResources"/>
Instead, open it
<fmt:bundle basename="ApplicationResources">
and close it
</fmt:bundle>
when you no longer need it, possibly at the end of your JSP. Nest your
<fmt:message key="login.title"/>
tags inside it.

Creating web dynamic project with Eclipse EE IDE

I am a newbie trying to create a simple web application using JSP and Mysql.
I am using following thing:
Eclipse EE IDE
Tomcat 7.0
Dtabase: MySQL
MySQL Connector
In my page I have row user add row dynamically that s work fine. I want my values should get stored in database after submit.I have searched lot on net but not find anything which can solve problem.I am giving code of current page.I know have to do connection also however I tried but not succeeded.
Thanks in advance.
JSP & Javascript:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#add").click(function() {
$('#mytable tbody>tr:last').clone(true).insertAfter('#mytable tbody>tr:last');
$('#mytable tbody>tr:last #name').val('');
$("#mytable tbody>tr:last").each(function() {this.reset();});
return false;
});
});
</script>
</head>
<body>
<form>
<a id="add" href="javascript:void(0)">Add another Credit card</a>
<table id="mytable" width="300" border="1" cellspacing="0" cellpadding="2">
<tbody>
<tr class="person">
<td><input type="text" name="name" id="name" /></td>
<td><input type="button" value="name" /></td>
<td><select>
<option>value1</option>
<option>value2</option></select>
<td><input type="text" name="name" id="name" /></td>
</tr>
</tbody>
</table>
<input type="submit" value="submit">
</form>
Logout
</body>
</html>

jsp not passing UTF-8 data in proper format

I want JSP pages to support UTF8 data I am able to localization with struts2 and jsp but when I take data from user on jsp in local language the information is not going in action in proper format it is passing some grabled data.
Here is my jsp code :------
<%# page language="java" contentType="text/plain; charset=UTF-8"
pageEncoding="UTF-8"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%#page import="java.util.*"%>
<%# taglib uri="/struts-tags" prefix="s"%>
<!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/plain; charset=UTF-8">
<title><s:text name="global.addnewcustomer"/></title>
<script type="text/javascript" src="http://localhost:9090/AMCMSWeb/basic/validation/Login.js">
</script>
</head>
<body>
<h2 align="center"><s:text name="global.fillinfo"/></h2>
<s:form action="addcustomeraction" method="post" acceptcharset="UTF-8">
<table align="center" border="1" bgcolor="pink" bordercolor="gray">
<tr>
<td><s:text name="global.custName"/></td>
<td>:</td>
<td><s:textfield name="custName" size="15"></s:textfield></td>
<td><s:text name="global.custMidleName"/></td><td>:</td><td><s:textfield name="custMidleName" size="15"></s:textfield></td>
<td><s:text name="global.custLastName"/></td><td>:</td><td><s:textfield name="custLastName" size="15"></s:textfield></td>
</tr>
<tr>
<td><s:text name="global.mobileNo"/></td><td>:</td><td><s:textfield name="mobileNo" size="15"></s:textfield></td>
<td><s:text name="global.phoneNo"/></td><td>:</td><td><s:textfield name="phoneNo" size="15"></s:textfield></td>
<td><s:text name="global.toDate"/> <s:label>(mmm/dd/yyyy)</s:label></td><td>:</td><td><s:textfield name="toDate" size="15" readonly="true">
<s:param name="value">
<s:date name="new java.util.Date()" format="MM/dd/yyyy"/>
</s:param>
</s:textfield></td>
</tr>
<tr>
<td><s:text name="global.atPost"/></td><td>:</td><td><s:textarea name="atPost" cols="15" rows="3"></s:textarea></td>
</tr>
<tr>
<td><s:text name="global.taluka"/></td><td>:</td><td><s:select list="#{'Miraj':'Miraj','Haveli':'Haveli'}" name="taluka" headerKey="-1" headerValue="Select Taluka" ></s:select></td>
<td><s:text name="global.district"/></td><td>:</td><td><s:select list="#{'Sangli':'Sangli','Pune':'Pune'}" name="district" headerKey="-1" headerValue="Select District"></s:select></td>
</tr>
<tr>
<td><s:text name="global.state"/></td>
<td>:</td>
<td><s:select list="#{'Maharashtra':'Maharashtra','Karnataka':'Karnataka'}" name="state" headerKey="-1" headerValue="Select State" onchange="list_districts()"></s:select></td>
<td><s:text name="global.country"/></td><td>:</td><td><s:select list="#{'India':'India'}" name="country" headerKey="-1" headerValue="Select Country" ></s:select></td>
</tr>
<tr>
<td><s:text name="global.pinCode"/></td>
<td>:</td>
<td><s:textfield name="pinCode" type="" size="15"></s:textfield></td>
</tr>
</table>
<table align="center" >
<tr>
<td><s:submit name="s" key="global.proceed"/></td>
<td><input type="button" name="cancel" value=" X "></td>
</tr>
</table>
</s:form>
</body>
</html>
The character encoding specified in the page (or in the web.xml) is applied to the following phases of an HTTP communication:
Preparing / sending the Request from the Client to the Server
Receiving / reading the Request in the Server
Preparing / sending the Response from the Server to the Client
Receiving / reading the Response in the Client
The Application Server is the only responsible for the phase 2.
You need to look for your specific application server settings, to alter the default character encoding (that could easily be ISO-8859-1), and alter it to work in UTF-8.
For example, in Tomcat you would need to edit the conf/server.xml file, by adding the URIEncoding="UTF-8" parameter to the <Connector>, for example from
<Connector port="8090" />
to
<Connector port="8090" URIEncoding="UTF-8"/>
In the Apache Wiki there is a nice list of things to check to make sure all your components are running in UTF-8:
What can you recommend to just make everything work? (How to use UTF-8
everywhere).
Using UTF-8 as your character encoding for everything is a safe bet.
This should work for pretty much every situation.
In order to completely switch to using UTF-8, you need to make the
following changes:
Set URIEncoding="UTF-8" on your in server.xml. References: HTTP
Connector,
AJP
Connector.
Use a character encoding filter with
the default encoding set to UTF-8
Change all your JSPs to include charset name in their contentType. For example, use <%#page contentType="text/html; charset=UTF-8" %>
for the usual JSP pages and <jsp:directive.page
contentType="text/html; charset=UTF-8" /> for the pages in XML syntax
(aka JSP Documents).
Change all your servlets to set the content type for responses and to include charset name in the content type to be UTF-8. Use
response.setContentType("text/html; charset=UTF-8") or
response.setCharacterEncoding("UTF-8").
Change any content-generation libraries you use (Velocity, Freemarker, etc.) to use UTF-8 and to specify UTF-8 in the content
type of the responses that they generate.
Disable any valves or filters that may read request parameters before your character encoding filter or jsp page has a chance to set
the encoding to UTF-8. For more information see
http://www.mail-archive.com/users#tomcat.apache.org/msg21117.html.

Firefox rewrites GET request

Background: I have a Java webserver class (NanoHTTPD) which my application extends by adding sessions, page templates, authentication, and dynamic content. I wanted to make the application self contained and not rely on anything but SQLite. It is meant to run with only Java and SQLite installed.
Now for my problem. I am testing a simple index page with only Logout link. If a user goes to the index without logging in, they are HTTP redirected to the Login page. When they POST the credentials and are validated, it returns them to the index page. If you click the Logout link in Firefox, my application only receives a URI to the index('/'). Following the same process in Chrome it logs the user out as expected. I do no modify the GET URL at any point; I only run uri.equals("/logout/"). Is there any reason why Firefox would interpret this link differently?
Login HTML:
<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">
<html>
<head>
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<div>
<p></p>
<form method="post" action="/login/">
<table>
<tr>
<td>
<label for="username">Username</label>
<input type="text" name="username" id="username" maxlength="100" />
</td>
<td>
<label for="password">Password</label>
<input type="password" name="password" id="password" />
</td>
<td></td>
<td>
<input type="submit" value="Sign In" />
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
Index HTML:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Index</title>
</head>
<body>
<h1>Index</h1>
Logout
</body>
</html>
The code for matching URL's is just .equals() and the only code running before this is NanoHTTPD which is located here: https://github.com/elonen/nanohttpd/blob/master/NanoHTTPD.java
One more thing, I have made a page, "test.html", to which I copied the source from above. Firefox does not handle it correctly either and stays on the page without giving a File Not Found error. Additionally, if I change the page to use "/logout1/", everything works.

Categories