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.
Related
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.
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.
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>
Here's my code that I am using to display data from the database.
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<%# page import="java.io.*,java.util.*,java.sql.*"%>
<!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">
<title>Book List</title>
</head>
<body>
<sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/BookTracker" user="root" password="school" />
<sql:query dataSource="${snapshot}" var="result">
SELECT * from BookTrackerSystem;
</sql:query>
<table border="1" width="100%">
<tr>
<th>Book ID</th>
<th>Book Name</th>
<th>Book Author</th>
<th>Book Genre</th>
<th>Book Description</th>
<th>Book Due Date</th>
<th>Book Status</th>
<th>Full Name</th>
</tr>
<c:forEach var="row" items="${result.rows}">
<tr>
<td><c:out value="${row.id}" /></td>
<td><c:out value="${row.bookName}" /></td>
<td><c:out value="${row.bookAuthor}" /></td>
<td><c:out value="${row.bookGenres}" /></td>
<td><c:out value="${row.bookDesc}" /></td>
<td><c:out value="${row.bookDueDate}" /></td>
<td><c:out value="${row.bookStatus}" /></td>
<td><c:out value="${row.fullname}" /></td>
</tr>
</c:forEach>
</table>
</body>
</html>
What I want to do, using a simple textbox I want to be able to filter the table displayed using the code above, I believe I can do it using jQuery but is jQuery compatible with Expression language or is there any other method I can make use of?
Despite the fact almost nobody will support you in what you are doing :) .. I mean all is in JSP..
The exact answer is the following:
Add an html form with a textbox.
Add code that reads the textbox value as request.getParameter().
http://www.tutorialspoint.com/jsp/jsp_form_processing.htm
Add the value from (2) into your query <sql:param value="..." /> and add where clause. Perhaps, you will need IF for two cases: a) filtering off - no text value and no where b) filtering on - text value exists and where used.
This will be a server-side processing (filtering) though.
If you want client-side filtering consider using DataTables http://www.datatables.net/
Seems like JSPs, Velocity, Freemarker etc. can offer so-called "inner templating": I can describe an outer template, then define inner parts. Like this (simplified):
main.jsp:
<html>
<body>
<div class="container">
<%# include file="menu.jsp"%>
<%# include file="content.jsp"%>
</div>
</body>
</html>
So I can define menu.jsp and content.jsp and all works just fine. But here outer block has references to inners. Not very suitable for me.
I'm looking for technology for Java, that can let me implement something like this:
some_block.jsp:
<template file="main_template.jsp">
<div>
... my content here
</div>
</template>
main_template.jsp:
<html>
<body>
<div class="container">
<inner_content />
</div>
</body>
</html>
I. e. vice versa - inner blocks have references to outer. Is it possible with JSPs? If not - what should I use with Spring MVC?
EDIT: Why it be more comfortable for me: when Controller receives a request, it detects what view it should render. So I can render, for example, feedback form:
feedback.jsp:
<template file="main_template.jsp">
<form> ... feedback form content here ... </form>
</template>
or a product page product.jsp:
<template file="main_template.jsp">
<div> ... product page content here ... </div>
</template>
and there is no need to describe page structure for every kind of pages, and there is no need to pass any parameters to outer template to render content correctly. And even no need in dynamic compilation - all pages are just an implicit set of precompiled servlets.
I created a JSP taglib you might be interested in:
http://www.inamik.com/projects/webframes/
Here is how I would implement your request using the WebFrames taglib
Notice that this is an exact 1-to-1 correlation to your example, but unlike your example, WebFrames allows you to create unlimited place-holders so you could have things like dynamic titles, css-includes, right-channel content, left-navs, etc.
feedback.jsp
<%# page language="java" %>
<%# taglib prefix="wf" uri="/WEB-INF/tld/webframes.tld" %>
<wf:render file="main_template.jsp">
<wf:section name="inner_content">
<form> ... feedback form content here ... </form>
</wf:section>
</wf:render>
product.jsp
<%# page language="java" %>
<%# taglib prefix="wf" uri="/WEB-INF/tld/webframes.tld" %>
<wf:render file="main_template.jsp">
<wf:section name="inner_content">
<div> ... product page content here ... </div>
</wf:section>
</wf:render>
main_template.jsp
<%# page language="java" %>
<%# taglib prefix="wf" uri="/WEB-INF/tld/webframes.tld" %>
<html>
<body>
<div class="container">
<wf:render section="inner_content" />
</div>
</body>
</html>
Official WebFrames Example
Below is the full example set from the website showing how you can create/populate multiple place-holders:
http://www.inamik.com/projects/webframes/examples/simpleexample.jsp
Start by looking at the source for the example main page:
main http://www.inamik.com/projects/webframes/examples/simpleexample.jsp.txt
<%# page language="java" %>
<%# taglib prefix="wf" uri="/WEB-INF/tld/webframes.tld" %>
<wf:render file="frame.jsp">
<wf:section name="title">WebFrames Simple Example Page</wf:section>
<wf:section name="header" file="headersection.html" />
<wf:section name="footer" file="footersection.html" />
<wf:section name="body">
This page is a composite of the following sub-pages. Click the links below to see
the jsp/html that makes up each sub-page.
<UL>
<LI>simpleexample.jsp</LI>
<LI>frame.jsp</LI>
<LI>headersection.html</LI>
<LI>footersection.html</LI>
</UL>
</wf:section>
</wf:render>
See how this defines the content sections without defining the layout.
If you look at the layout ('frame.jsp') You'll see it doesn't know what content is going to be displayed, it just creates place-holders for the content:
frame http://www.inamik.com/projects/webframes/examples/frame.jsp.txt
<%# page language="java" %>
<%# taglib prefix="wf" uri="/WEB-INF/tld/webframes.tld" %>
<HTML>
<HEAD><TITLE><wf:render section="title" /></TITLE></HEAD>
<BODY>
<TABLE width="100%">
<!-- Header -->
<TR>
<TD>
<TABLE width="100%">
<TR>
<TD>
<wf:render section="header" />
</TD>
</TR>
</TABLE>
</TD>
</TR>
<!-- Body -->
<TR>
<TD>
<TABLE width="100%">
<TR>
<TD>
<wf:render section="body" />
</TD>
</TR>
</TABLE>
</TD>
</TR>
<!-- Footer -->
<TR>
<TD>
<TABLE width="100%">
<TR>
<TD>
<wf:render section="footer" />
</TD>
</TR>
</TABLE>
</TD>
</TR>
</BODY>
</HTML>
header http://www.inamik.com/projects/webframes/examples/headersection.html.txt
<!-- BEGIN headersection.html -->
<TABLE bgcolor="#00C0C0" width="100%">
<TR>
<TD nowrap="nowrap" align="LEFT">
<H1>WebFrames Sample Header</H1>
</TD>
</TR>
</TABLE>
<!-- END headersection.html -->
footer http://www.inamik.com/projects/webframes/examples/footersection.html.txt
<!-- BEGIN footersection.html -->
<TABLE bgcolor="#00C0C0" width="100%">
<TR>
<TD nowrap="nowrap" align="CENTER">
Copyleft (c) SampleFooter Inc.
</TD>
</TR>
</TABLE>
<!-- END footersection.html -->
This pattern provides a much more flexible solution that creates components that are re-usable and easier to maintain.
NOTES
This was based on earlier work by David Geary:
http://www.javaworld.com/javaworld/jw-12-2001/jw-1228-jsptemplate.html
Also, if you're willing (or considering) non-JSP solutions, I wrote a Java-based template engine in the spirit of PHP's Smarty template engine
https://github.com/iNamik/iNamik-Template-Engine
I also have a 'Frames' taglib for this engine, which isn't on GitHub yet.
I've found two solutions - complex one and very simple and cute:
First one is SiteMesh:
http://wiki.sitemesh.org/
And second is a custom tag from Claudius Hauptmann:
http://code.google.com/p/jsp-decorator
Both work fine, so I hope this can help to someone else.
This is straight forward to do using JSP TAG files.
First create your template tag:
/WEB-INF/tags/template/maintemplate.tag
<%# tag body-content="scriptless" %>
<html>
<body>
<div class="container">
<jsp:doBody/>
</div>
</body>
</html>
And then create your JSP page that uses the template tag:
/some_page.jsp
<%# page %>
<%# taglib prefix="template" tagdir="/WEB-INF/tags/template" %>
<template:maintemplate>
<div>
... my content here
</div>
</template:maintemplate>
Your JSP page uses the maintemplate.tag and passes it a block of body content. The maintemplate.tag chooses where to render that body content.