Java web 实现登录注册的功能
这是我第一次用java web来编写程序,可能对这门语言了解尚浅,但我也希望能帮助上像我这样的小白。这个程序运用到了数据库的连接,以及一些软件的配置,对于初学者应该是有帮助的,闲话少说,现在开始第一步!
软件的选择
因为本人一直学习的是ASP.NET,现在学习JAVA,甚至还要用这个语言来写系统,我表面看得若无其事,内心其实慌得一批。那么,我要用什么软件来编写Java了,这里有几款软件可以给大家参考:
- IntelliJ IDEA,简称IDEA,IntelliJ在业界被公认为最好的java开发工具之一。它的旗舰版本还支持HTML,CSS,PHP,MySQL,Python等。免费版只支持Java等少数语言。它最突出的功能自然是调试(Debug),可以对Java代码,JavaScript,JQuery,Ajax等技术进行调试。
有关于此软件的用法在菜鸟教程里面有详细的介绍,这里就不班门弄斧了。
下载地址:https://www.jetbrains.com/idea/
菜鸟教程链接:http://www.runoob.com/w3cnote/intellij-idea-usage.html - MyEclipse,是在eclipse 基础上加上自己的插件开发而成的功能强大的企业级集成开发环境,主要用于Java、Java EE以及移动应用的开发。MyEclipse的功能非常强大,支持也十分广泛,尤其是对各种开源产品的支持相当不错。
下载地址:http://www.myeclipsecn.com/download/
目前现在最为常用的就是这两种,我身边的小伙伴都用第一种软件,对于我这种初学者就喜欢用eclipse,全凭大家喜好,但以下我要讲的内容是有关于eclipse软件以及他的相关配置。 - Tomcat,大家都知道我们所做的系统是JAVA WEB ,所以一点很重要,需要联网,那就是需要一个服务器,那就需要汤姆猫(Tomcat)了,他是Apache 服务器的扩展,但运行时它是独立运行的。实际上目前最新版本已经9.0.14,大家可以在官网下载。
下载地址:http://tomcat.apache.org/
4、Mysql,他在 WEB 应用方面,是最好的 RDBMS (Relational Database Management System,关系数据库管理系统) 应用软件。
下载地址:https://www.mysql.com/downloads/
开始项目
废话说了一大篇,那有关于配置遇到的常见问题我会在后面给大家详细介绍。那开始项目吧,首先我们登录系统,需要数据吧,那我们就需要创建数据库,(有关于mysql的用法,之后再详细介绍),假设我们建立一个表Student,里面存储了用户名,密码等字段。如图所示:
然后我们把饭菜做好了,我们得需要一双筷子啊。所以,我们需要在eclipse里面进行数据库连接了,数据库连接大同小异,其实都是一些小套路。
DBUtil.java
public class DBUtil {
public static String classname="com.mysql.jdbc.Driver";
public static String username="root";
public static String password="123";//第一点
public static String url="jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false";//第二点
static{
try{
Class.forName("com.mysql.cj.jdbc.Driver");
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
}
public static Connection getConnection(){
Connection conn=null;
try{
conn=DriverManager.getConnection(url, username, password);
}
catch(SQLException e){
e.printStackTrace();
}
return conn;
}
public static void CloseDb(ResultSet rs,PreparedStatement stm,Connection conn){
if(rs!=null){
try{
rs.close();
}catch(SQLException e){
e.printStackTrace();
}
}
if(stm!=null){
try{
stm.close();
}
catch(SQLException e){
e.printStackTrace();
}
if(conn!=null){
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
//测试是否有这个链接
public static void main(String[] args){
Connection conn= DBUtil.getConnection();
System.out.println(conn);
if(conn!=null){
System.out.println("ok");
}else{
System.out.println("sorry");
}
}
}
第一点需要注意的地方是你安装mysql时候设置的密码,我设置的是123,大家要根据实际进行修改。
第二点需要注意他的url,大家可能百度到一些地址并没有这么长一串,因为我尝试过之前简短的语句,运行会报错。我也是百度才得知的答案,大家可以试试。在后面也有个测试连接,来测试你是否连接成功。
登录和注册的具体代码
Login.jsp
<div class="header">
<h2>学生信息管理系统</h2>
</div>
<div class="left">
</div>
<div class="center">
<form method="post" action="/MessageManage/Loginservlet">//第一点
<div id="text">
<input type="text" id="username" name="username" placeholder="用户名" required="required"/>
</div>
<div id="text">
<input type="text" id="password" height="30px" name="password" placeholder="密码" required="required"/>
</div>
<br/>
<div id="button">
<input type="submit" value="登录" />
<input type="button" value="注册" onClick="location.href='Register.jsp'"/>//第二点
</div>
</form>
</div>
第一点,大家要注意form表单的action的传值方向,因为我的项目名字是叫MessageManage,然后与另一文件Loginservlet联系。
第二点,这是是注册界面的跳转,触发了onclick的事件,一点要记住里面的页面是用的单引号。
Loginservlet.java(注意这里是servlet文件)
private void Loginservlet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
// TODO Auto-generated constructor stub
//doGet(request, response);
request.setCharacterEncoding("GB2312");
response.setContentType("text/html;charset=GB2312");//第一点
//获取账号和密码
String username = request.getParameter("username");
String password = request.getParameter("password");
Studentdao sdao = new Studentdao();//第二点
//对账号和密码进行判断
boolean result = sdao.Login(username, password);
//判断输入正确
if(result){
PrintWriter pw=response.getWriter();
pw.print("<script language='javascript'>alert('success');window.location.href='/MessageManage/Main.jsp'</script>");
}else{
//没有找到对应的账号和密码,返回重新登录
PrintWriter pw=response.getWriter();
pw.print("<script language='javascript'>alert('用户名或密码错误');window.location.href='/MessageManage/Login.jsp'</script>");
}
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String path = request.getServletPath();
Loginservlet(request,response);
}
第一点需要注意的是这里是文本转换,不加这个代码,会出现最讨厌的画面——乱码。
第二点,这里有个Studentdao的类,里面主要是对数据表进行相关操作。登录,相当于对数据表进行了查询,如果输入的数据与数据库的相符,那么就登录成功,注册就相当于增加,将数据添加到数据表中。
Studentdao.java
public class Studentdao {
Connection conn=DBUtil.getConnection();
PreparedStatement stm=null;
ResultSet rs=null;
public boolean Login(String username,String password){
String sql="select * from studentinfo where UserName='"+username+"'and PassWord='"+password+"'";//第一点
try{
stm=conn.prepareStatement(sql);
rs=stm.executeQuery();
if(rs.next()){
return true;
}
}
catch(SQLException e){
e.printStackTrace();
}
finally{
DBUtil.CloseDb(rs, stm, conn);
}
return false;
}
//注册
public void Register(String username,String password,String name,String profession,String mail,String phone){
Connection conn=DBUtil.getConnection();
String sql="insert into studentinfo(username,password,name,profession,mail,phone)values(?,?,?,?,?,?)";//第二点
int num=0;
PreparedStatement stm=null;
try{
stm=conn.prepareStatement(sql);
stm.setString(1, username);
stm.setString(2, password);
stm.setString(3, name);
stm.setString(4, profession);
stm.setString(5, mail);
stm.setString(6, phone);
num=stm.executeUpdate();
}
catch(SQLException e){
e.printStackTrace();
}
}
}
第一点需要注意的是sql语句的书写,单引号的输入,很容易导致程序出错。
第二点需要注意的是插入语句的values后面的问号,一定要对应你的字段的个数,其次,因为我的数据表时添加了很多字段,大家可以任意删减字段,达到你的预期效果。
register.jsp
<div class="header">
<h2>注册新用户</h2>
</div>
<div class="left">
</div>
<div class="center">
<form method="post" action="/MessageManage/Registerservlet">
<div id="text">
<input type="text" id="user-name" name="username" required="required" placeholder="请输入账号">
</div>
<div id="text" >
<input type="password" id="user-name" name="password" required="required" placeholder="请输入密码">
</div>
<div id="text" >
<input type="text" id="user-name" name="name" required="required" placeholder="请输入姓名">
</div>
<div id="text" >
<input type="text" id="user-name" name="profession" required="required" placeholder="请输入专业">
</div>
<div id="text" >
<input type="text" id="user-name" name="mail" required="required" placeholder="请输入邮箱">
</div>
<div id="text" >
<input type="text" id="user-name" name="phone" required="required" placeholder="请输入手机号">
</div>
<div id="button">
<input type="submit" value="提交"/>
<input type="button" value="返回登录" onclick="location.href='Login.jsp';"/>
</div>
</form>
</div>
Registerservlet.java
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 {
// TODO Auto-generated method stub
//doGet(request, response);
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
//获取注册信息
String username = request.getParameter("username");
String password = request.getParameter("password");
String name = request.getParameter("name");
String profession = request.getParameter("profession");
String email = request.getParameter("mail");
String phone = request.getParameter("phone");
Studentdao studentdao = new Studentdao();
//将注册信息存入数据库,再返回登录
studentdao.Register(username,password,name,profession,email,phone);
response.sendRedirect("/MessageManage/Login.jsp");
}
运行效果展示
Login
register
注册用户以及注册成功
登录也可以实现,大家可以根据代码自行操作!
CSS样式表
虽然不知道大家喜欢不喜欢这个样式,但还是分享出来。
common.css
body{
background:url(./images/zz.jpg) no-repeat;
background-size:100% 100%;
width:1000px;
}
.header{
width:20%;
heigth:100px;
color:white;
text-align:center;
padding-top:180px;
padding-left:180px;
}
.left{
width:600px;
height:400px;
float:left;
}
.center{
width:300px;
height:100px;
float:left;
position:absolute;
top:280px;
left:200px;
}
#button input{
width: 80px;
padding:3px;
margin:2px 3px 0 3px;
background: #66FF66;
color: #fff;
border-radius:10px;
border: 1px solid transparent;
cursor:pointer;
color:rgba(255,255,255,0.7);
}
#text input{
height:25px;
border-radius:10px;
background:#CCEEFF;
margin:2px 2px 2px 0;
border:1px solid transparent;
}
#text input:hover{
border:1px solid blue;
}
#button input:hover{
color:white;
}
大家要在jsp的文件中写上一句话,注意是在里面,才能运用该样式: <link href="common.css" rel="stylesheet" type="text/css" />
好了,本文章就这样结束了。有关于常见问题,下次再详细的介绍,也希望这篇文章能帮助到大家。