Online FAQ (Service side section)

Source: Internet
Author: User

The main tools used to write the service-side are the Sevlert language, and the MyEclipse programming tool.

Register part of the core code:

Package rico_cliente;

Import java.io.IOException;
Import Java.io.PrintWriter;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;
Import Java.util.Iterator;

Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;

Import Org.omg.CORBA.Object;

Import Net.sf.json.JSONArray;
Import Net.sf.json.JSONObject;
/*
* http://localhost:8080/maiji/register.do?data={"LoginName": "Tom", "Interesting": ["Game", "Sports"]}
*/
public class Registerservlet4androidclient extends HttpServlet {
Public Registerservlet4androidclient () {
Super ();
}
public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
This.dopost (request, response);
}
public void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Request.setcharacterencoding ("UTF-8");
/*
* Data= {
* "LoginName": "Tom"
* "Interesting": ["Game", "Sports"]
* }
*/
String data = Request.getparameter ("Data");
SYSTEM.OUT.PRINTLN (data);
Parsing of/*json Data | stripping */
/*
* Json-lib Core code for parsing JSON data
*/
Jsonobject object = jsonobject.fromobject (data);

String loginName = object.getstring ("LoginName");
SYSTEM.OUT.PRINTLN ("Registered login name is:" +loginname);
String Loginpassword = object.getstring ("Loginpassword");
SYSTEM.OUT.PRINTLN ("Registered login password is:" +loginpassword);

Jsonarray array = Object.getjsonarray ("interesting");
SYSTEM.OUT.PRINTLN ("Registered hobbies are:");
if (array! = NULL) {
for (Object Obj:array) {
System.out.print (obj.tostring () + "\ T");
//}
//}
for (int i = 0; i < array.length (); i++) {
//
//}
/*
* Business Processing
*/

Request.setcharacterencoding ("gb2312");
Response.setcontenttype ("text/heml;charset=gb2312");
Response.setcharacterencoding ("UTF-8");
Response.setcontenttype ("Text/html;charset=utf-8");
ResultSet rs = null;
Statement stmt = null;
Connection CNN =null;

/*
* Response: Encapsulate JSON results
* {
* "Result": "Success"
* "ErrorMsg": ""
*
* }
* {
* "Result": "Failed"
* "ErrorMsg": "Server processing error, registration failed"
*
* }
*/


/**
* Return value
* */

PrintWriter Out=null;

try{
String user = "root";
String password = "123456";
Out=response.getwriter ();
Resultjsonbean Jsonbean = new Resultjsonbean ();
Class.forName ("Com.mysql.jdbc.Driver");
SYSTEM.OUT.PRINTLN ("Load Database driver successfully");
CNN = Drivermanager.getconnection ("jdbc:mysql://localhost:3306/androidclent", user, password);
stmt = Cnn.createstatement ();
rs = Stmt.executequery ("select * from cloentregister where emp_name = '" +loginname+ "'");
if (Rs.next ()) {
Jsonbean.setresult ("failed");
Jsonbean.seterrormsg ("Login name has been used");
Jsonobject obj = Jsonobject.fromobject (Jsonbean);
System.out.println (Obj.tostring ());
Out.print (Obj.tostring ());
}else{
Stmt.executeupdate ("INSERT into Cloentregister (emp_name,emp_passward) VALUES ('" +loginname+ "', '" +loginpassword+ "') ");
Jsonbean.setresult ("Success");
Jsonbean.seterrormsg ("");
Jsonobject obj = Jsonobject.fromobject (Jsonbean);
System.out.println (Obj.tostring ());
Out.print (Obj.tostring ());
}

/**************************************************/
rs = Stmt.executequery ("SELECT * from Cloentregister");
while (Rs.next ()) {
System.out.println (rs.getstring ("Emp_name"));
//
//}





Jsonbean.setresult ("Success");
Jsonbean.seterrormsg ("");

}catch (SQLException e) {
E.printstacktrace ();
}catch (ClassNotFoundException e) {
E.printstacktrace ();
}finally{
if (out!=null) {
Out.close ();
}
try {
if (rs! = null) {
Rs.close ();
rs = null;
}
if (stmt! = null) {
Stmt.close ();
stmt = null;
}
if (CNN = null) {
Cnn.close ();
CNN = NULL;
}
} catch (SQLException e) {
E.printstacktrace ();
}

}
}
}

Login Part Core Code:

Package rico_cliente;

Import java.io.IOException;
Import Java.io.PrintWriter;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;

Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
/*
* HTTP://LOCALHOST:8080/MAIJI/BEGIN.DU
*http://localhost:8080/maiji/begin.du?loginname=tom&loginpassword=123
* */
public class Rico_sndddb extends HttpServlet {

Public Rico_sndddb () {
Super ();
}


public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {

System.out.println ("get");
This.dopost (request, response);

}

public void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {

System.out.println ("post");
Request.setcharacterencoding ("UTF-8");

try {
Thread.Sleep (5000);
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
//}

/*
* Take value
* */
String loginname=request.getparameter ("LoginName");
String loginpassword=request.getparameter ("Loginpassword");
System.out.println (LoginName);
System.out.println (Loginpassword);

Avoid garbled characters
Response.setcharacterencoding ("UTF-8");
/*
* The default
* text/html
* */
Response.setcontenttype ("Text/html;charset=utf-8");
/**
* Return value
* */
Request.setcharacterencoding ("gb2312");
Response.setcontenttype ("text/heml;charset=gb2312");
Response.setcharacterencoding ("UTF-8");
Response.setcontenttype ("Text/html;charset=utf-8");
ResultSet rs = null;
Statement stmt = null;
Connection CNN =null;
PrintWriter Out=null;

try{
Out=response.getwriter ();
/**
* Landing Business judgment * *
boolean bool = false;
String name;
String Spassword;
String user = "root";
String password = "123456";
Class.forName ("Com.mysql.jdbc.Driver");
SYSTEM.OUT.PRINTLN ("Load Database driver successfully");
CNN = Drivermanager.getconnection ("jdbc:mysql://localhost:3306/androidclent", user, password);
stmt = Cnn.createstatement ();
rs = Stmt.executequery ("SELECT * from Cloentregister");
while (Rs.next ()) {
Name=rs.getstring ("Emp_name");
Spassword=rs.getstring ("Emp_passward");
if (loginname.equals (name) &&loginpassword.equals (Spassword)) {
Login Right
Bool=true;
Break
}

}

if (bool) {
Login Right
Out.print ("Success");

}
else{
Login failed
Out.print ("failed");
}
}catch (SQLException e) {
E.printstacktrace ();
}catch (ClassNotFoundException e) {
E.printstacktrace ();
}finally{
if (out!=null) {
Out.close ();
}
}

}

}

Online FAQ (Service side section)

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.