最容易出错到点
- JDBC连接数据库却忘记导包:mysql-connector-java-5.1.7-bin.jar
- 使用Servlet却忘记导tomcat的包:servlet-api.jar,这个包在tomcat/lib中找到
- 自己手写Servlet却忘记到web.xml中写配置
登陆页面实现
<html>
<head>
<meta charset="UTF-8">
<title>Insert title heretitle>
head>
<body>
登陆对应的Servlet
package vipdownload;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Login_Servlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
ArrayList<String> list = new ArrayList<>();
Enumeration<String> parameterNames = req.getParameterNames();
while (parameterNames.hasMoreElements()) {
String key = (String) parameterNames.nextElement();
list.add(req.getParameter(key));
}
Connection conne = JDBC_util.getConnetion();
PreparedStatement pst = null;
String sql = "select * from user where username=? && password=?";
try {
pst = conne.prepareStatement(sql);
pst.setString(1, list.get(0));
pst.setString(2, list.get(1));
ResultSet resultSet = pst.executeQuery();
if (resultSet.next()) {
resp.setStatus(302);
resp.setHeader("Location", "download.html");
} else {
resp.getWriter().write("登录失败");
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
JDBC_util.release(conne, pst);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
注册页面
<html>
<head>
<meta charset="utf-8">
<title>会员注册title>
<script>
function checkTips(spanId, msg) {
var span = document.getElementById(spanId);
span.innerHTML = msg;
}
function checkUserName() {
var username = document.getElementById("username").value;
var span = document.getElementById("span_username");
if (username.length > 13 || username.length < 3) {
span.innerHTML = "用户名长度不符合";
} else {
span.innerHTML = "用户名可用";
return true;
}
}
function checkPassWord() {
var password = document.getElementById("password").value;
var span = document.getElementById("span_password");
if (password.length < 6) {
span.innerHTML = "密码长度小于6位";
} else {
span.innerHTML = "密码可用";
return true;
}
}
function checkRePassWord() {
var repassword = document.getElementById("repassword").value;
var password = document.getElementById("password").value;
var span = document.getElementById("span_repassword");
if (password == repassword) {
span.innerHTML = "密码一致"
return true;
} else {
span.innerHTML = "密码不一致"
}
}
function checkFrom() {
var flag = checkUserName() && checkPassWord() && checkRePassWord();
if (flag) {
return flag;
}
return false;
}
script>
head>
<body>
<form action="Register_Servlet" method="get" onsubmit="checkFrom()">
用户名:<input type="text" id="username" name="username"
onfocus="checkTips('span_username','用户名取值为3-12个字符')"
onblur="checkUserName()" onkeyup="checkUserName()"><span id="span_username">span><br>
密码:<input type="text" name="password"
id=password onfocus="checkTips('span_password','密码长度必须大于等于6')"
onblur="checkPassWord()" onkeyup="checkPassWord()"><span id="span_password">span><br>
确认密码:<input type="text" name="repassword"
id=repassword onfocus="checkTips('span_repassword','密码必须一致')"
onkeyup="checkRePassWord()" onblur="checkRePassWord()"><span id="span_repassword">span><br>
<input type="submit" value="注册">
form>
body>
html>
注册对应到Servlet
package vipdownload;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Enumeration;
import java.util.Timer;
import java.util.TimerTask;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Register_Servlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
Connection conne = JDBC_util.getConnetion();
PreparedStatement pst = null;
ResultSet rs = null;
try {
String sql = "select * from user where username=?";
pst = conne.prepareStatement(sql);
pst.setString(1, req.getParameter("username"));
System.out.println(req.getParameter("username"));
rs = pst.executeQuery();
if (rs.next()) {
resp.getWriter().write("用户名已存在!");
} else {
resp.getWriter().write("注册成功!");
String username = req.getParameter("username");
String password = req.getParameter("password");
String insertSQL = "insert into user values(null,?,?,null)";
PreparedStatement pst2 = conne.prepareStatement(insertSQL);
pst2.setString(1, username);
pst2.setString(2, password);
pst2.executeUpdate();
resp.setStatus(302);
resp.setHeader("Location", "download.html");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBC_util.release(conne, pst, rs);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
下载页面
<html>
<head>
<meta charset="UTF-8">
<title>Insert title heretitle>
head>
<body>
我很幸福.mkva><br>
歌词.cfga><br>
美女.jpga><br>
body>
html>
下载页面对应的Servlet
package vipdownload;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DownLoad_Servlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String filename = req.getParameter("name");
String path = getServletContext().getRealPath("DownLoad/" + filename);
filename = URLEncoder.encode(filename, "UTF-8");
resp.setHeader("Content-Disposition", "attachment;filename=" + filename);
InputStream is = new FileInputStream(path);
OutputStream os = resp.getOutputStream();
byte[] arr = new byte[1024 * 8];
int len = 0;
while ((len = is.read(arr)) != -1) {
os.write(arr, 0, len);
}
os.close();
is.close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}