登录
Servlet代码
Servlet代码所在文件夹位置如下截图所示
登陆的servlet代码文件命名为LoginServlet.java
package webconnect;
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 javax.servlet.http.HttpSession;
import sqlconnect.OptSql;
/**
* Servlet implementation class LoginServlet
*/
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8"); //将Servlet的编码设为utf-8,以便输出中文
PrintWriter printwriter=response.getWriter();
//从index.html中获取网页的输入值
String tt=request.getParameter("telephone");
String ps=request.getParameter("pwd");
System.out.println(tt+"\t"+ps);
//连接数据库
OptSql oo=new OptSql();
//向函数传参,取得值
int cc=oo.sql_login(tt, ps);
System.out.println(cc);
if(cc==1) {
String userid=oo.get_id(tt);
String username=oo.get_username(tt);
HttpSession session=request.getSession();
session.setAttribute("userid",userid);
session.setAttribute("username", username);
session.setAttribute("telephone", tt);
response.sendRedirect("needs.jsp");
}else {
printwriter.write("没有该账户或密码错误");
}
oo.close();
}
}
html代码
前端html和jsp的代码,以及需要的图片,都放在同一文件夹中,如截图所示
登陆的前端代码文件名为index.html,会作为打开的第一个页面。
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>login</title>
<style>
</style>
</head>
<body>
<p><img src="title.jpg" width="266" height="64" alt=""/></p>
<div id="container" style="width: 95%;">
<div id="advertise" style="float: left;">
<img src="adv12306.jpg" width="887" height="533" alt=""/>
</div>
<div id="loginbar" style="height:350px;width:500px;float:left;position: relative; top:70px;">
<p id="zhdl" style="width: 100%; text-align: center; color:#8C570F; font-size:200%;font-weight: 500;">账号登陆</p>
<form method="post" action="LoginServlet" id="telephone" style="width:100%; text-align: center; font-size: 150%; height: 50px;color: #8C570F;font-weight: 500;">
手机号:<input type="text" name="telephone"><br><br>
密 码:<input type="password" name="pwd"><br>
<input type="submit" value="确认登陆">
</form>
<a href="signup.html" id="gotosu" style="text-align: center;float: right;">注册账号</a>
</div>
</div>
</body>
</html>
代码内使用的title.jpg图片如下:
图片adv12306.jpg如下:
注册
Servlet代码
在SignupServlet.java中
package webconnect;
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 sqlconnect.OptSql;
@WebServlet("/SignupServlet")
public class SignupServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8"); //将Servlet的编码设为utf-8,以便输出中文
PrintWriter printwriter=response.getWriter();
String gname=request.getParameter("username");
String gid=request.getParameter("id");
String gtele=request.getParameter("telephone");
String gpwd=request.getParameter("pwd");
String gcpwd=request.getParameter("confirm_pwd");
if(gpwd.equals(gcpwd)) {
OptSql oo=new OptSql();
int result=oo.sql_signup(gid, gname, gtele, gpwd);
oo.close();
System.out.println("result="+result);
if(result==1) {
printwriter.write("<h1>恭喜您,注册成功<br></h1>");
printwriter.write("<a href=\"index.html\">去登陆</a>");
}else {
printwriter.write("注册失败:(");
}
}else {
printwriter.write("两次密码输入不同");
}
}
}
html代码
代码在signup.html中
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>signup</title>
</head>
<body>
<p><img src="title.jpg" width="266" height="64" alt=""/></p>
<div id="container" style="width: 50%; height: 600px; position: absolute; top: 94px; left: 55px; border: 1px solid blue">
<p id="zhdl" style="width: 100%; text-align: center; color:#2D687A; font-size:200%;font-weight: 700;">
请输入真实信息注册</p>
<form method="post" action="SignupServlet" id="bb" style="text-align: center; font-size: 150%; height: 50px;color: #428498;font-weight: 500;"><br>
姓名:<input type="text" name="username"><br><br>
身份证号:<input type="text" name="id"><br><br>
手机号:<input type="text" name="telephone"><br><br>
密 码:<input type="password" name="pwd"><br><br>
确认密码:<input type="password" name="confirm_pwd"><br><br>
<input type="submit" value="注册"><br><br>
</form>
</div>
</body>
</html>
12306购票系统以往文章链接:
【Java+JSP+MySql】12306购票系统(一)项目介绍
【Java+JSP+MySql】12306购票系统(二)数据库
【Java+JSP+MySql】12306购票系统(三)JDBC
如果你觉得这篇文章对你有用的话,麻烦点赞,收藏,关注一下。
感谢您的阅读,您的支持是对我最大的鼓励~