Methods for submitting data to a Tomcat server using post methods _java

Source: Internet
Author: User
Tags tomcat server

In my previous article, I described using get to submit data to a Tomcat server, which would introduce the use of post to submit data to a server, which is exactly the same as creating a Web project with a get way, in just a few different places. I'll just introduce the different places, the first difference is that the submissions are different, so modify the code in the Loginservlet.java

Package com.fyt.org; 
Import java.io.IOException; 
Import Java.io.OutputStream; 
Import Java.io.PrintWriter; 
Import javax.servlet.ServletException; 
Import Javax.servlet.http.HttpServlet; 
Import Javax.servlet.http.HttpServletRequest; 
Import Javax.servlet.http.HttpServletResponse; 
public class Loginservlet extends HttpServlet {public loginservlet () {super (); 
public void Destroy () {Super.destroy (); ///Use Get method to submit data to server public void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception , IOException {}///submit data to server using POST method public void DoPost (HttpServletRequest request, httpservletresponse response) thro 
WS Servletexception, IOException {//Get user name sent from browser String username = request.getparameter ("username"); 
Gets the password sent over from the client String password = request.getparameter ("password"); Convert a username to a byte array using ISO8859-1 encoding//And then use Utf-8 to convert a byte array to a string username = new String (Username.getbytes ("iso8859-1"), "Utf-8") 
; Print the username and password in the console System.out.println ("UserName= "+ username); 
System.out.println ("password=" + password); 
Get an output stream outputstream OS = Response.getoutputstream (); If the username and password are entered correctly if (username) && "123". Equals (password)) {//Send the word Fu Fa to the browser os.write ("login succeeded". GetBytes ( 
"Utf-8")); 
else {//Send the string to the browser os.write ("Login Failed". GetBytes ("Utf-8")); } 
} 
}

The second place to be modified is index.jsp, which modifies the code in index.jsp to the following code

<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%> <% String Path = Request.getcontextpath () 
; 
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/"; %> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >  

Deploy the project to a Tomcat server after the modification is complete, and you can deploy it as a reference to my blog. Submit data to Tomcat server using GET method
When the deployment is complete, enter http://192.168.1.102:8080/WebProject/index.jsp in the browser, and the interface shown in the browser below indicates that the project was successfully deployed to the browser


Enter a small log in the username, enter 123 in the password, the login succeeds when the login is displayed in the browser, because I set the correct username in the server is a small log, the correct password is 123


Logon failure is displayed when an entry error occurs in a username or password

about using post to submit data to the Tomcat server method to introduce so many people, I hope to help you!

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.