package com.Servlet;
import java.io.IOException;
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 java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import com.DBTool.DBUtil;
@WebServlet("/Servlet")
public class Login extends HttpServlet {
private static final long serialVersionUID = L;
/**
* @see HttpServlet#HttpServlet()
*/
public Login() {
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
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String ID = request.getParameter("ID");
String PW= request.getParameter("PW");
boolean type=false;
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
try
{
Connection con=DBUtil.getConnection();
Statement stmt=con.createStatement();
//mysql数据库中的数据表,表名叫:demotable ,需要自己预先在数据库中进行创建,包含相应的字段和记录。
String sql="select * from mysql.demotable where uid="+ID+" and pwd="+PW;
ResultSet rs=stmt.executeQuery(sql);
while(rs.next())
{
type=true;
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
DBUtil.Close();
out.print(type);
out.flush();
out.close();
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>web</display-name>
<servlet>
<display-name>Login</display-name>
<servlet-name>Login</servlet-name>
<servlet-class>com.Servlet.Login</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/Login</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form id="from" action="Login" method="post"> <table> <tr><td>用户名</td><td><input type="text" name="ID"></td></tr> <tr><td>密码</td><td><input type="password" name="PW"></td></tr> <tr><td colspan="2" align="center"><input type="submit" value="登陆"/></td></tr> </table> </form> </body> </html>
package com.DBTool;
import java.sql.*;
public class DBUtil {
//其中mysql是数据库名称,在mysql57版本的数据库中已经预先新建完成;3306是mysql数据库的端口号。
private static String url="jdbc:mysql://localhost:3306/mysql";
//com.mysql.jdbc.Driver是mysql-connector-java-5.1.40中的驱动包路径
private static String driverClass="com.mysql.jdbc.Driver";
//mysql的账号和密码是在安装mysql中进行设置的,这里拿来用即可。
private static String username="root";
private static String password="123456";
private static Connection conn;
//装载驱动
static{
try{
Class.forName(driverClass);
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
}
//获取数据库连接
public static Connection getConnection(){
try{
conn=DriverManager.getConnection(url,username,password);
}
catch(SQLException e){
e.printStackTrace();
}
return conn;
}
//建立数据库连接
public static void main(String[] args){
Connection conn=DBUtil.getConnection();
if(conn!=null){
System.out.println("数据库连接成功");
}
else{
System.out.println("数据库连接失败");
}
}
//关闭数据库连接
public static void Close(){
if(conn!=null){
try{
conn.close();
}
catch(SQLException e){
e.printStackTrace();
}
}
}
}
public void SendByHttpClient(final String id, final String pw){
new Thread(new Runnable() {
@Override
public void run() {
try {
HttpClient httpclient=new DefaultHttpClient();
HttpPost httpPost=new HttpPost("http://web应用部署服务器上的IP地址:/HttpClientDemo/Login");//服务器地址,指向Servlet
List<NameValuePair> params=new ArrayList<NameValuePair>();//将id和pw装入list
params.add(new BasicNameValuePair("ID",id));
params.add(new BasicNameValuePair("PW",pw));
final UrlEncodedFormEntity entity=new UrlEncodedFormEntity(params,"utf-8");//以UTF-8格式发送
httpPost.setEntity(entity);
HttpResponse httpResponse= httpclient.execute(httpPost);
if(httpResponse.getStatusLine().getStatusCode()==200)//在200毫秒之内接收到返回值
{
HttpEntity entity=httpResponse.getEntity();
String response=EntityUtils.toString(entity1, "utf-8");//以UTF-8格式解析
Message message=new Message();
message.what=USER_LOGIN;
message.obj=response;
handler.sendMessage(message);使用Message传递消息给线程
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有