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 9097201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:02:56+00:00 2026-06-17T00:02:56+00:00

I have a servlet which takes data from the form and inserts it in

  • 0

I have a servlet which takes data from the form and inserts it in the database.In servlet I make use of beans and get/set data via beans.Once the insert happens ,user is redirected to the view which shows as how exactly the page looks.This page is JSP. Now when I do this page refresh(jsp),it again does form data insert into the database.Now I need to prevent this from happening.What I was thinking was rather than redirecting it to jsp,let me redirect to another servlet and from that servlet let me do the get request and forward it to jsp for view.With this approach, I have refactor my jsp and I have lot many jsp which will be impacted.

Is their any other way I can prevent insert into the database when page refresh is done ?

I also checked on database side assuming that I can add unique constraints, I do have some unique columns that gets generated via initial servlet,so on each insert values are different,so I cannot really use the solution on DB side.

Can someone suggest me if there exists work arounds for this ?

Update:Adding code which does insert, I am refreshing the page manually.Seems like this is bug which I missed out to consider earlier.It does the insert correctly in the database and shows correctly all data in the view jsp.When the view jsp is refreshed, I get double inserts in the database.

Update .removed the code.Will post relevant parts of the code rather than whole code.

Issue is resolved.

 javax.servlet.http.HttpSession session = request.getSession();            
        if (session.getAttribute("insertflag") == null) {
            mybean sf = new mybean();
            sf.insert(form);
            session.setAttribute("insertflag", insertid);
            RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/test/showtestform.jsp");                
            rd.forward(request, response);
        } else {                
            RequestDispatcher rd = request.getRequestDispatcher("/errorpage.jsp");
            rd.forward(request, response);
        }

In error page jsp I remove the attribute set and then provide the link in that page to come back again to home page.

  • 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-17T00:02:57+00:00Added an answer on June 17, 2026 at 12:02 am

    A simple workaround can be that you put a flag in Session; whenever insert is done.. And before inserting always check session if this flag is present.

    In the servlet;
    Before inserting into the database; make following check:

    if (session.getAttribute("recordInsertedSuccessfully") == null )
    {
       //proceed with insertion
       //after inserting into the database we should do :
       session.putAttribute("recordInsertedSuccessfully","true");
    } else {
       //case of form re-submission
    }
    

    If there is a possibility of a valid scenario of user inserting two of these records ( might be user browsing and then again inserting record in same table ; which is a valid scenario ) ; then we need to also add following code before the form is shown to user:

    session.removeAttribute("recordInsertedSuccessfully");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this managed bean which takes two values from database's table import java.io.Serializable;
I have a servlet which takes us to an existing jsp, say home.jsp. This
In an embedded Jetty container I have a servlet which pushes data to the
I have a Java servlet from which I need to send few notifications to
I have a servlet which handles http get requests that I'd like to be
Greetings, I have a servlet which pulls an action parameter from a querystring. Based
I have a login JSP which takes username / password from the user in
We have a servlet which occupies more virtual memory on the server due to
We have a servlet which occupies more virtual memory on the server as it
I have a Java servlet which calls another software (say S) over a TCP

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.