Servlet(Server Applet)是Java Servlet的简称,称为小服务程序或服务连接器,用Java编写的服务器端程序,主要功能在于交互式地浏览和修改数据,生成动态Web内容。Servlet 的主要功能在于交互式地浏览和修改数据,生成动态 Web 内容。这个过程为:
1、客户端发送请求至服务器端;
2、服务器将请求信息发送至 Servlet;
3、Servlet 生成响应内容并将其传给服务器。响应内容动态生成,通常取决于客户端的请求;
4、服务器将响应返回给客户端。
package com.scong.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.junit.Test;
import com.mysql.jdbc.Driver;
public class JdbcHello {
private ResultSet resultSet;
private Statement statement;
private Connection connection;
@Test
public String[] loginStrings(){
String[] nameStrings=new String[100];
int i=0;
//注册驱动 注册mysql驱动
try {
DriverManager.registerDriver(new Driver());
//mysql服务器的地址 jdbc:子协议(mysql)://服务器ip地址:端口(默认3306)/数据库名字
String url="jdbc:mysql://xxx.xxx.xxx.xxx:3306/login";
//访问mysql服务端的用户名
String username="root";
//访问mysql服务端的密码
String password="123456";
connection = DriverManager.getConnection(url,username, password);
statement = connection.createStatement();
String sql="select * from user;";
resultSet = statement.executeQuery(sql);
//调用next()方法移动光标到下一行
while(resultSet.next()){
String name=resultSet.getString(1);
String pwString=resultSet.getString(2);
//System.out.println(name);
nameStrings[i]=name+"##"+pwString;
i++;
}
//return nameStrings;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{ //关闭连接
try {
resultSet.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return nameStrings;
}
}
package com.scong.servlet;
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;
import com.scong.db.JdbcHello;
/**
* Servlet implementation class LoginTest
*/
@WebServlet("/LoginTest")
public class LoginTest extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LoginTest() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String usernameString = request.getParameter("username");
String passwordString = request.getParameter("password");
JdbcHello jdbcHello = new JdbcHello();
String[] name = jdbcHello.loginStrings();
PrintWriter writer = response.getWriter();
if (usernameString == null || passwordString == null
|| usernameString.length() == 0 || passwordString.length() == 0) {
writer.print("false!");
} else {
int i = 0;
String temp = usernameString + "##" + passwordString;
for (i = 0; i < name.length; i++) {
if (temp.equals(name[i])) {
writer.print("true");
break;
}
}
if (i == name.length) {
writer.print("login false!");
}
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.junit.Test;
import com.mysql.jdbc.Driver;
public class JDBCEqual {
private ResultSet resultSet;
private Statement statement;
private Connection connection;
@Test
public String[] RegisterEqual(){
String[] nameStrings=new String[100];
int i=0;
//注册驱动 注册mysql驱动
try {
DriverManager.registerDriver(new Driver());
//mysql服务器的地址 jdbc:子协议(mysql)://服务器ip地址:端口(默认3306)/数据库名字
String url="jdbc:mysql://xxx.xxx.xxx.xxx:3306/login";
//访问mysql服务端的用户名
String username="root";
//访问mysql服务端的密码
String password="123456";
connection = DriverManager.getConnection(url,username, password);
statement = connection.createStatement();
String sql="select * from user;";
resultSet = statement.executeQuery(sql);
//调用next()方法移动光标到下一行
while(resultSet.next()){
String name=resultSet.getString(1);
//System.out.println(name);
nameStrings[i]=name;
i++;
}
//return nameStrings;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{ //关闭连接
try {
resultSet.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return nameStrings;
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.junit.Test;
import com.mysql.jdbc.Driver;
public class JDBCRegister {
private ResultSet resultSet;
private Statement statement;
private Connection connection;
@Test
public boolean Register(String usernamestring,String passwordstring){
boolean success=false;
//注册驱动 注册mysql驱动
try {
DriverManager.registerDriver(new Driver());
//mysql服务器的地址 jdbc:子协议(mysql)://服务器ip地址:端口(默认3306)/数据库名字
String url="jdbc:mysql://47.95.193.106:3306/login";
//访问mysql服务端的用户名
String username="root";
//访问mysql服务端的密码
String password="123456";
connection = DriverManager.getConnection(url,username, password);
statement = connection.createStatement();
String sql="insert into user values ('"+usernamestring+"','"+passwordstring+"');";
statement.executeUpdate(sql);
success=true;
//return nameStrings;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{ //关闭连接
// try {
// resultSet.close();
// } catch (SQLException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
try {
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return success;
}
}
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;
import com.scong.db.JDBCEqual;
import com.scong.db.JDBCRegister;
import com.scong.db.JdbcHello;
/**
* Servlet implementation class Register
*/
@WebServlet("/Register")
public class Register extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Register() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException ,NullPointerException {
String usernameString = request.getParameter("username");
String passwordString = request.getParameter("password");
JDBCEqual jdbcEqual=new JDBCEqual();
JDBCRegister jdbcRegister=new JDBCRegister();
String[] name=jdbcEqual.RegisterEqual();
PrintWriter writer = response.getWriter();
if (usernameString == null || passwordString == null
|| usernameString.length() == 0 || passwordString.length() == 0) {
writer.print("false!");
} else {
int i = 0;
String temp = usernameString;
for (i = 0; i < name.length; i++) {
if (temp.equals(name[i])) {
writer.print("equal");
break;
}
}
if (i==name.length) {
boolean success=jdbcRegister.Register(usernameString, passwordString);
writer.print(success);
}
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>ServletTestdisplay-name>
<welcome-file-list>
<welcome-file>index.htmlwelcome-file>
<welcome-file>index.htmwelcome-file>
<welcome-file>index.jspwelcome-file>
<welcome-file>default.htmlwelcome-file>
<welcome-file>default.htmwelcome-file>
<welcome-file>default.jspwelcome-file>
welcome-file-list>
<servlet>
<servlet-name>loginservlet-name>
<servlet-class>com.scong.servlet.LoginTestservlet-class>
servlet>
<servlet-mapping>
<servlet-name>loginservlet-name>
<url-pattern>/loginurl-pattern>
servlet-mapping>
<servlet>
<servlet-name>registerservlet-name>
<servlet-class>com.scong.servlet.Registerservlet-class>
servlet>
<servlet-mapping>
<servlet-name>registerservlet-name>
<url-pattern>/registerurl-pattern>
servlet-mapping>
web-app>