Java EE servlet handles request for form submission

Source: Internet
Author: User

1. Create a new servlet page under the source package named Loginservlet:

Package weinidingservlet; The package in which the servlet resides

Import java.io.IOException;
Import Java.io.PrintWriter;
import javax.servlet.ServletException;
import Javax.servlet.annotation.WebServlet;
import Javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import Javax.servlet.http.HttpServletResponse; Packages that are imported automatically when you generate a servlet page
import java.sql.*; SQL statement packages that you need to add yourself
Import java.util.*;
Import Java.util.logging.Level;
Import Java.util.logging.Logger;
import weinidingservlet.myconnection; Database connections and action packs that you need to add yourself
import weinidingjavadata.wndmembers; Entity classes generated by the database that you need to add yourself
/**
*
* @author Spring Fei
*/



@WebServlet (name = "Loginservlet", Urlpatterns = {"/loginservlet"})
public class Loginservlet extends HttpServlet {
Public Loginservlet () {
Super ();
}


/**

* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet Request
* @param response servlet response
* @throws servletexception If a servlet-specific error occurs
* @throws IOException If an I/O error occurs
*/
protected void DoPost (HttpServletRequest request, httpservletresponse response)//dopost function primarily handles data submitted by the form's post method, The premise must be if the front-end form form has a method= "post"
Throws Servletexception, IOException {
try {
Response.setcontenttype ("Text/html;charset=utf-8");
String name=request.getparameter ("username"); The GetParameter method of the Request property gets the data field of the front-end form submission
String password=request.getparameter ("password");
wndmembers member=new wndmembers (); New an entity class generated by the database
member.setwndmembersname (name); Call the Set method in the entity class to assign the get data to the entity class
MEMBER.SETWNDMEMBERSPW (password);
myconnection my=new myconnection (); New class object for a database connection
My.init (); To invoke the initialization method of a database connection object
My.insert (member); Execute Insert Method


processrequest (request, response);                                            //Used To return the corresponding front-end page
} catch (SQLException ex) {
Logger.getlogger (LoginServlet.class.getName ()). log ( Level.severe, NULL, ex);
}
}

/**
* Returns A short description of the servlet.
*
* @return a String containing servlet description
*/
protected void ProcessRequest (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Try (PrintWriter out = Response.getwriter ()) {
/* TODO output your page here. Use following sample code. */
Out.println ("<! DOCTYPE html> ");
Out.println ("Out.println ("Out.println ("<title>servlet loginservlet</title>");
Out.println ("Out.println ("<body>");
Out.println ("Out.println ("</body>");
Out.println ("}
}

<editor-fold defaultstate= "collapsed" desc= "httpservlet methods. Click on the + sign on the left to edit the code. >
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet Request
* @param response servlet response
* @throws servletexception If a servlet-specific error occurs
* @throws IOException If an I/O error occurs
*/
@Override
protected void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
ProcessRequest (request, response);
}

/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet Request
* @param response servlet response
* @throws servletexception If a servlet-specific error occurs
* @throws IOException If an I/O error occurs
*/
@Override

Public String Getservletinfo () {
Return "Short description";
}//</editor-fold>

}

Java EE servlet handles request for form submission

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.