A problem with jsp requested parameters in tomcat
I've created a JSP project with netbeans 7.0 and tomcat 7.0 too with no开发者_如何学JAVA problem.
When I export it to a WAR and deploy it into my Tomcat server this line doesn't work:
<% if (request.getParameter("submit").contains("اضافه")) { ...
but these ones works:
<% if (request.getParameter("emplist") != null) { ...
How is this problem caused and how can I solve it?
In TOMCAT_HOME/conf/web.xml
, you can add a filter to enable character encoding to UTF-8
Add the following:
<filter>
<filter-name>setCharacterFilter</filter-name>
<filter-class>org.apache.catalina.filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>false</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>setCharacterFilter</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>
If you want to URL to be encoded to include UTF-8, you will have to add a URIEncoding="UTF-8"
to your Tomcat Connector.
Find your HTTP connector (in TOMCAT_HOME/conf/server.xml
, usually you can find a port 8080
assigned) and add the attribute URIEncoding
as above, as follows:
<Connector port="8080" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true"
URIEncoding="UTF-8"
/>
I hope this helps.
Is your deployment tomcat version same as your development one ?
This might be related to request encoding problem . you should set encoding to UTF-8 either by calling the setCharacterEncoding of HttpServletRequest object or by setting utf8 charset in your form .
Like following. You also can create the custom filter class to set the encoding of the request to UTF-8
public class UnicodeFilter implements Filter {
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
req.setCharacterEncoding("UTF-8");
chain.doFilter(req, res);
}
public void init(FilterConfig config) throws ServletException {
//Get init parameter
}
public void destroy() {
//add code to release any resource
}
}
精彩评论