简体   繁体   中英

Display new HTML page in servlet

I have an HTML page which posts its form data (DB credentials) to a servlet. In the servlet, I create a connection using the credentials.

Now I want to display an HTML page which will accept the details of a record to be entered. I want to then retrieve this data and enter it into the DB. I know how to display a new HTML page using response.getWriter(), but now do I set this page to post its data again to the same servlet? How does that work? How should I code the servlet?

Will this second post start a new servlet instance? I don't understand, any help would be appreciated, thanks!

You shouldn't let Servlet write entire HTML in response, you should use JSP, set the data from servlet forward request to JSP and it will render HTML with your dynamic content

See servlet wiki for complete example with explanation

On the whole your approach is quite clumsy, You can slightly modify it.

I have an HTML page which posts its form data (DB credentials) to a servlet. 
In the servlet, I create a connection using the credentials.

Establish the connection. Check whether the connection is established

    Now I want to display an HTML page which will accept the details of a record to be entered. 
   I want to then retrieve this data and enter it into the DB

If established redirect it to the 2nd html page you need Create a html page to accept the inputs from the user and post it to the servlet . You can even post it to the same servlet and handle them using the input field or you can even post it to another servlet

  Will this second post start a new servlet instance?

No the web container doesnt create a new object every time , rather it shares the same instance untill the app is running

And i dont know why you wanted to display the html content inside the servlet . JSP is meant for that purposes only .

Hope it helps !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM