JSP cannot be accessed directly through a browser. It must be redirected through servlet, but the & lt; iframe & gt; tag in a jsp file contains access to another jsp. The iframe browser is compatible.

Source: Internet
Author: User

JSP cannot be accessed directly through a browser. It must be redirected through servlet. However, the <iframe> label in a jsp file contains the access problem of another jsp. The iframe browser is compatible.

Today, when doing the job encountered a problem, before we write jsp code is put under the webcontext directory, not put in its subdirectory under the WED-INF directory, all lead to the browser can directly access jsp, so there is a lot of security issues, after understanding, the correct way is to put the jsp file under the WED-INF directory, and then jump through the Controller. If the browser directly accesses this in the WED-INF directory under the jsp will appear 404 page, can not find the jsp file.

1. First of all, let's take a look at test1.jsp under the WEB-INF directory is not directly accessible, put under WebContext is able to access directly. (1.1, 1.2)

    

Figure 1.1 (jsp file directory)

Figure 1.2 (the browser directly accesses the jsp below the WEB-INF)

Figure 1.3 (the browser directly accesses the jsp under the WebContext directory)

2. But we can access test1.jsp through servlet jump.

The following is the servlet code:

1 package com. servlet; 2 3 import java. io. IOException; 4 import javax. servlet. servletException; 5 import javax. servlet. annotation. webServlet; 6 import javax. servlet. http. httpServlet; 7 import javax. servlet. http. httpServletRequest; 8 import javax. servlet. http. httpServletResponse; 9 10 @ WebServlet ("/TestServlet") 11 public class TestServlet extends HttpServlet {12 private static final long serialVersionUID = 1L; 13 public TestServlet () {14 super (); 15} 16 protected void service (HttpServletRequest request, 17 HttpServletResponse response) throws ServletException, IOException {18 request. setCharacterEncoding ("UTF-8"); 19 response. setContentType ("text/html; charset = UTF-8"); 20 request. getRequestDispatcher ("/WEB-INF/test1.jsp") 21. forward (request, response); 22} 23 24}
Then you can access test1.jsp.



Figure 2.1 (access test1.jsp under the WEB-INF directory via servlet jump)

3. however, we sometimes encounter this situation when using the <iframe> label in jsp to include another jsp to be accessed by colleagues, but the included jsp cannot be directly accessed by the browser, all intelligence is placed under the web-inf directory, but the problem arises. Through the servlet jump, you can only access the outer jsp, And the jsp contained in it will have a 404 error.

Figure 3.1 (the jsp Layer Code contains a jsp file)

Figure 3.2 (jsp files included in the access jsp results cannot be displayed)
In this case, we need to configure jsp in web. xml to redirect access through the server, and the client cannot directly access the specific Code as follows:
<servlet>   <servlet-name>goto</servlet-name>   <jsp-file>/WEB-INF/jsp/msgs.jsp</jsp-file>  </servlet>  <servlet-mapping>   <servlet-name>goto</servlet-name>   <url-pattern>/msgs</url-pattern>  </servlet-mapping>

Then, access the iframe property src = "msgs.



Although this is a very small problem, I encountered a problem when I learned jsp, So I recorded it and I think it is still rewarding. If there is anything wrong, I am a newbie,
I hope you will be grateful for your correction!


 

    

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.