Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8479369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:00:13+00:00 2026-06-10T19:00:13+00:00

In my web application, I use the .load() function in JQuery, to load some

  • 0

In my web application, I use the .load() function in JQuery, to load some JSP pages inside a DIV.

$("#myDiv").load("chat.jsp");

In chat.jsp, no Java codes is executed unless this client has Logged in, means, I check the session.

String sessionId = session.getAttribute("SessionId");
if(sessionId.equals("100")){
  //execute codes
}else{
  //redirect to log in page
}

Those java codes that will be executed, they will out.println(); some HTML elements.

I don’t want the client to write /chat.jsp in the browser to access this page, as it will look bad, and the other stuff in the main page won’t be there, and this could do a harm to the web app security.

How can I restrict someone from accessing chat.jsp directly, but yet keep it accessible via .load() ?

UPDATE:

JavaDB is a class that I made, it connects me to the Database.

This is chat.jsp

<body>

    <%

        String userId = session.getAttribute("SessionId").toString();
        if (userId != null) {
            String roomId = request.getParameter("roomId");
            String lastMessageId = request.getParameter("lastMessageId");
            JavaDB myJavaDB = new JavaDB();
            myJavaDB.Connect("Chat", "chat", "chat");
            Connection conn = myJavaDB.getMyConnection();
            Statement stmt = conn.createStatement();
            String lastId = "";
            int fi = 0;
            ResultSet rset = stmt.executeQuery("select message,message_id,first_name,last_name from users u,messages m where u.user_id=m.user_id and m.message_id>" + lastMessageId + " and room_id=" + roomId + " order by m.message_id asc");
            while (rset.next()) {
                fi = 1;
                lastId = rset.getString(2);
    %>
    <div class="message">
        <div class="messageSender">
            <%=rset.getString(3) + " " + rset.getString(4)%>
        </div>
        <div class="messageContents">
            <%=rset.getString(1)%>
        </div>
    </div>
    <%            }
    %>
    <div class="lastId">
        <% if (fi == 1) {%>
        <%=lastId%>
        <% } else {%>
        <%=lastMessageId%>
        <% }%></div>

    <% if (fi == 1) {%>
    <div class="messages">
    </div> 
    <% }
        } else {
            response.sendRedirect("index.jsp");
        }%>
</body>

Guys I don’t know what Filter means.

UPDATE

If I decided to send a parameter that tells me that this request came from Jquery.

.load("chat.jsp", { jquery : "yes" });

And then check it in chat.jsp

String yesOrNo = request.getParameter("jquery");

Then they can simply hack this by using this URL.

/chat.jsp?jquery=yes

or something like that..

UPDATE

I tried Maksim’s advice, I got this when I tried to access chat.jsp.

enter image description here

Is this the desired effect?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T19:00:16+00:00Added an answer on June 10, 2026 at 7:00 pm

    In order to achieve this in my application I check for X-Requested-With field in http header the client sends to my page in its request. If its value is XMLHttpRequest, then it’s very likely that it came from an ajax request (jQuery appends this header to its requests), otherwise I don’t serve the page. Regular (direct) browser requests will leave this header field blank.

    In ASP.Net it looks like this, you will have to change your code slightly for JSP:

    if (Request.Headers["X-Requested-With"] != "XMLHttpRequest")
    {
         Response.Write("AJAX Request only.");
         Response.End();
         return;
    }
    

    UPD: After quick googling your code will probably be something like this

    if(!request.getHeader("X-Requested-With").equals("XMLHttpRequest")){
        out.println("AJAX Request only.");
        out.flush(); 
        out.close(); 
        return; 
    }
    

    UPD2: Looks like request.getHeader("X-Requested-With") returns null in your case change the condition to something like this:

    String ajaxRequest = request.getHeader("X-Requested-With");
    if(ajaxRequest == null || !ajaxRequest.equals("XMLHttpRequest")){
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

2 months ago I was writing a web-application which use some classes to draw
I'm working on a project to convert an existing Java web application to use
I have load balanced web servers My application has a function that allows the
A function in my application does the following: Capture Web Page using Snoopy Load
I decided to use knockout.js for my web application and have some concerns regarding
I have a Web Application that use Hibernate 3.0. When I restart my database
In a .Net web application I use the public DataRow[] Select(string filterExpression) method in
I'm developing a web application that use Spring Security for authentication of users. I
So I am writing a web application for use within my organization. The application
I am attempting to enhance a internal web application we use. I am using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.