javaServlet+tomcat+mysql简单登陆注册web测试环境搭建

  • 配置Eclipse的web运行服务器
    window–>preference–>server–>runtime enviroment–>add 然后选择自己本机的tomcat。前提是本地要下载有tomcat
  • eclipse创建Dynamic web project
    输入我的工程名LoginServlet,然后next 记得选上创建web.xml 也可以后面再创建。

  • 在webcontent下面新建一个index.html
    就是两个form一个登陆,一个用户注册。对应两个servlet

  
<html xmlns="http://www.w3.org/1999/xhtml">  
   <head>  
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />  
       <title>登录title>  
    head>  
    <body>  

       <form action="http://localhost:8080/LoginServlet/LoginServlet" method="post">  
          用户:<input type="text" name="username" /><br/>  
           密码:<input type="password" name="password" /><br/>  
          <input type="submit" value="登录" />  

      form>  
        <form action="http://localhost:8080/LoginServlet/RegisterServlet" method="post">  
          用户:<input type="text" name="username" /><br/>  
           密码:<input type="password" name="password" /><br/>  
          <input type="submit" value="注册" />  

      form>  
  body>  
html> 

在javaResource目录下创建对应的LoginServlet和RegisterServlet
LoginServlet代码如下,其中调用了UserRegister类里面的LonginCheck()方法,对页面输入的用户名和密码进行验证


//引入所需要的包
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;


public class LoginServlet extends HttpServlet {
    //重写doGet方法
    public void doGet(HttpServletRequest request,
            HttpServletResponse response)
     throws ServletException,
            IOException {
        String username = request.getParameter("username");   
        String password = request.getParameter("password");      
        UserRegister ur = new UserRegister();
        boolean b = ur.LonginCheck(username, password);
        System.out.println(b);
        //服务器端打印信息


        response.setContentType("text/html;charset=GB18030");

        //返回html页面
        response.getWriter().println("");
        response.getWriter().println(""); 
        response.getWriter().println("登录信息");    
        response.getWriter().println("");    
        response.getWriter().println(""); 
        if(b){
        response.getWriter().println("欢迎【" + username + "】用户登录成功!!!");  
        }else
        {
            response.getWriter().println("登陆失败");   
        }
        response.getWriter().println("");    
        response.getWriter().println("");
        System.out.println("yeee");
        }                   
    //重写doPost方法
    public void doPost(HttpServletRequest request,
            HttpServletResponse response)
    throws ServletException,
            IOException {
        doGet(request, response);                   
    }  

}

RegisterServlet的代码如下
其中调用了UserRegister类里面的regist()方法进行注册。其实就是往数据库里面插入一条数据

//引入所需要的包
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;


public class RegisterServlet extends HttpServlet {
    //重写doGet方法
    public void doGet(HttpServletRequest request,
            HttpServletResponse response)
     throws ServletException,
            IOException {
        String username = request.getParameter("username");   
        String password = request.getParameter("password");      
        UserRegister tb = new UserRegister();
//      TestConnDb tb = new TestConnDb();
        UserBean ub =new UserBean();
        ub.setUserId(username);
        ub.setPassword(password);
        tb.setUserBean(ub);
        try {
            tb.regist();
            //服务器端打印信息
            //System.out.println("username=" + username);
            //System.out.println("password=" + password);
            //设置编码格式
            response.setContentType("text/html;charset=GB18030");

            //返回html页面
            response.getWriter().println("");
            response.getWriter().println(""); 
            response.getWriter().println("登录信息");    
            response.getWriter().println("");    
            response.getWriter().println(""); 
            response.getWriter().println(username + "注册成功");    
            response.getWriter().println("");    
            response.getWriter().println("");
            System.out.println("yeee");

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        }                   
    //重写doPost方法
    public void doPost(HttpServletRequest request,
            HttpServletResponse response)
    throws ServletException,
            IOException {
        doGet(request, response);                   
    }     
}

然后就是操作数据库的类,也是直接创建在SRC下面就OK了,主要是连接数据库,进行注册和密码校验操作。这里用到了UserBean类。里面提供了一个测试的main方法,可进行测试连接数据库操作是否正确

import com.mysql.jdbc.Driver;
import java.sql.*;
public class UserRegister
{
        private UserBean userBean;
        private Connection con;
        //获得数据库连接。
        public UserRegister()
        {

             String url="jdbc:mysql://192.168.1.107/"+"test"+"?user="+"root"+"&password="+"test";

            try
            {

                Class.forName("com.mysql.jdbc.Driver").newInstance();
                        con = DriverManager.getConnection(url);
                }
                catch(Exception e)
                {
                        e.printStackTrace();
                }

        }
        //设置待注册的用户信息。
        public void setUserBean(UserBean userBean)
        {
                this.userBean=userBean;
        }
        //进行注册
        public void regist() throws Exception
        {
                String reg="insert into userinfo(userid,password)  values(?,?)";

                try
                {
                        PreparedStatement pstmt=con.prepareStatement(reg);
                        pstmt.setString(1,userBean.getUserId());
                        pstmt.setString(2,userBean.getPassword());
                        pstmt.executeUpdate();
                }
                catch(Exception e)
                {
                        e.printStackTrace();
                        throw e;
                }


        }
        //登陆校验
       public boolean LonginCheck(String username,String password){
           try {
               Statement stmt=con.createStatement();
               String sql="select * from  userinfo where userid='"+username+"';";
               ResultSet rs=stmt.executeQuery(sql);
            if(rs.next())
            {
                String passwordR=new String(rs.getString("password"));
//          System.out.println(passwordR);
                if(passwordR.equals(password)){
                    System.out.println(passwordR);
                    return true;
                }else{
                    return false;
                }
            }

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        return false;

       }
        //测试
//       public static void main(String[] args) {
//         UserRegister ur = new UserRegister();
////           UserBean ub = new UserBean();
////           ub.setUserId("te");
////           ub.setPassword("te1");
////           ur.setUserBean(ub);
////           try {
////            ur.regist();
////        } catch (Exception e) {
////            // TODO Auto-generated catch block
////            e.printStackTrace();
////        }
//         System.out.println(ur.LonginCheck("te", "te1"));
////           System.out.println(ur.LonginCheck("u1", "pwd1"));
//         
//         
//  }
}

userbean类的代码如下



public class UserBean
{
       private String userid;
       private String password;

        public void setUserId(String userid)
        {
                this.userid=userid;
        }
        public void setPassword(String password)

        {
                this.password=password;
        }
        public String  getUserId()
        {
                return this.userid;
        }
        public String getPassword()
        {
                 return this.password;
        }

}

最后是web-inf下面的web.xml配置了


<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
  <servlet>
    <servlet-name>MyServletservlet-name>
    <servlet-class>LoginServletservlet-class>


  servlet>
  <servlet-mapping>
    <servlet-name>MyServletservlet-name>
    <url-pattern>/LoginServleturl-pattern>
  servlet-mapping>
   <servlet>
    <servlet-name>MyServlet1servlet-name>
    <servlet-class>RegisterServletservlet-class>


  servlet>
  <servlet-mapping>
    <servlet-name>MyServlet1servlet-name>
    <url-pattern>/RegisterServleturl-pattern>
  servlet-mapping>
web-app>

在工程名上右键,run as –>run on server
运行结果:
javaServlet+tomcat+mysql简单登陆注册web测试环境搭建_第1张图片
注册:test1 密码:test11
javaServlet+tomcat+mysql简单登陆注册web测试环境搭建_第2张图片
登陆验证一下:
javaServlet+tomcat+mysql简单登陆注册web测试环境搭建_第3张图片
数据库验证userinfo里面插入了一条数据:
javaServlet+tomcat+mysql简单登陆注册web测试环境搭建_第4张图片

这样一个简单的登陆和注册的javaServle+tomcat+mysql的测试环境就搭建好了

备注:
搭建过程中遇到的一些问题:
1 没有将mysql-connector-java.jar拷贝到tomcat的lib下面导致连接数据库失败
搭建环境如果有用到第三方jar包,记得将对应的jar包拷贝到tomcat的lib下面
2 开始提示注册的servlet找不到,是因为没有在web.xml里面配置对应的servlet,页面与servlet的交互都应该在web.xml里面进行配置
3 连接不上mysql原因是mysql的root用户登录限定在了linux上,需要改成任意主机。具体实现可以百度一下

你可能感兴趣的:(jmeter性能测试,linux系统)