Java Web ClassProgram_1
问题描述:
简单的web登录程序
实现代码
jiemian.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="shujuku.jsp" method="post">
User Log<br>
UserName<input type="text" name="name">
<br>
PassWord<input type="password" name="passWord">
<input type="submit" name="submit" value="log">
</form>
</body>
</html>
shujuku.jsp
<%@page import="java.sql.*"%>
<%@ 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>
<%
try{
System.out.print("跳入");
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test";
String username = "******";
String password = "******";
String user_name=request.getParameter("name");
String user_pass=request.getParameter("passWord");
System.out.println(user_name+" "+user_pass);
if(user_name==""||user_pass==""){
out.print("name or pass can't be null");
}else{
Connection conn=DriverManager.getConnection(url, username, password);
if(conn!=null){
out.print("database success");
Statement sm=conn.createStatement();
ResultSet rs=sm.executeQuery("select * from class where userName='"+user_name+"' and userPass='"+user_pass+"'");
if(rs.next()){
response.sendRedirect("chenggong.html");
}
else{
response.sendRedirect("shibai.html");
}
conn.close();
}
else{
out.print("数据库连接失败");
}
}
}
catch(Exception e){
e.printStackTrace();
}
%>
</body>
</html>
chenggong.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>成功</h1>
</body>
</html>
shibai.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>失败</h1>
</body>
</html>