Quick solution to the garbled problem of jsp page requesting forwarding

  • 2021-11-02 01:58:15
  • OfStack

In recent projects, jsp+servlet was used to develop the project, but it was difficult due to the lack of proficiency in the background. Fortunately, the learning ability can be learned once and for all.

Today's problem: Garbled page after request forwarding

Due to special reasons-the form form uploaded by pictures cannot be submitted through ajax, so it must be submitted directly using form form. However, this will lead to a problem: the interaction between front and back office is not effective. Why doesn't it work well? This is not a question of function. As web developers, we should put the customer experience first. Therefore, we must put the information feedback to customers in a more important position. To put it bluntly, did you upload a picture successfully? Is the information I posted successfully uploaded? If the web page doesn't respond when you upload something, Or just jump to another page, He will think it is OK, but in fact it has not been uploaded to the server, for many reasons, such as my current network is not good, or the file is too large, or your file is not supported, but this is difficult for users who don't understand the code, and they will think that your website is really rubbish.

Therefore, we will try our best to think for our customers and convey the information they want to know to them.

Of course, ajax is very practical, but it is impossible to upload data through ajax in the case of file stream.

Thus request. getRequestDispacher ('url'). forward request forwarding and redirection comes in handy. Redirection can't share data, so you can only use request forwarding.

In this case, the files are uploadServlet and user. jsp pages. When uploading the shopping information, all my information has been inserted into the background, and I need to return a message for the user saying that you succeeded. Then the traditional way, request. setAttribute () is very practical, and it is OK to use request to obtain the value on jsp page to judge whether it is successful or not.

But after forwarding it back, the page was garbled. . .

After searching for information in many ways, I finally found a solution;

The filter is configured to filter all files.

• Paste as follows:


package com.java.Filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class EncodingFilter implements Filter
{

 public void destroy()
 {
 // TODO Auto-generated method stub

 }

 public void doFilter(ServletRequest req, ServletResponse resp,
  FilterChain chain) throws IOException, ServletException
 {
  HttpServletRequest request = (HttpServletRequest) req;
  HttpServletResponse response = (HttpServletResponse) resp;
  request.setCharacterEncoding("utf-8");
  response.setCharacterEncoding("utf-8");
  response.setContentType("text/html;charset=UTF-8");
  chain.doFilter(request, response);
 }

 public void init(FilterConfig arg0) throws ServletException
 {
 // TODO Auto-generated method stub

 }
}

• web. xml configuration:


<filter>
  <filter-name>EncodingFilter</filter-name>
  <filter-class>com.java.Filter.EncodingFilter</filter-class>
 </filter>
 <filter-mapping>
  <filter-name>EncodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>

Related articles: