javaweb 基于java Servlet登入 简单入门案例

项目流程

第一步:创建一个java webproject
第二步:创建三个界面,1,login.jsp 2 success.jsp 3 fail.jsp
第三步:更改新建界面编码格式,utf-8 默然编码格式会中文乱码。
第四步:把当前新建项目添加到tomcat服务器,并启动服务器,查看界面效果
第五步:浏览器访问http://127.0.0.1:8080/HelloServlet/login.jsp 127.0.0.1 本地ip 8080端口号,tomcat默认端口号 后面就是项目名称加指定界面名称
界面已经搭建好
开始重点 servlet
什么是Servlet?
一个Servlet就是java编程语言的一个类,被用来扩展服务器的性能,,对任何类型的请求产生响应,具体 www.baidu.com hha

servlet 有两个请求方式 get 和 post,我们界面采用post请求方式

第六步:整个案例已经完成,试着跑一遍,看看效果
登入按钮应该使用 submit
具体原因不明,我们调试一下代码。。。

java 里面字符串判断相等 错误案例 username=="sa" 正确用 username.equals("sa")

1 新建项目HelloServlet

点击 确定Finish

2 新建界面 login.jsp  success.jsp   fail.jsp

 

 点击 Finish

 

点击Finish

 

点击Finish

3 调整界面

 a.由于界面默认编码格式只支持英文,输入中文会乱码,我们更改成utf-8

修改过的效果

 

ps:刚才新建的三个界面都是

4: a:修改login.jsp 代码

 在body里面添加如下代码

<!--   采用post提交 -->
      <form action="logins" method="post">
    用户名:<input type="text" name="username"><br>
    密码:<input type="password" name="password"><br>
    <input type="submit">
    </form>

  b:success.jsp界面

  <div style="size:25px;">登入成功</div>

c:fail.jsp界面

  <div style="size:25px;">登入失败</div>

5,添加Servlet类

a,先新建一个包

b 新建Servlet

 

 

注意:我们只要选择doget dopost 方法都可以了

 

注意:红色框框代码

新建好Servlet类,用下面代码替换一下

package com.wilson.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class logins extends HttpServlet {

    /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doPost(request,response);
    }

    /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

          String username = request.getParameter("username");
          String password = request.getParameter("password");
        

        //由于没有数据库连接,写一个死的判断一下用户名密码
        if(username.equals("sa")&&password.equals("123")){
            System.out.println("登入成功");
            //跳转登入成功界面
            response.sendRedirect("success.jsp");
        }else{
            //否则,跳转我们定义的失败界面 System.out.println类似日志打印一下
            System.out.println("登入失败");
            response.sendRedirect("fail.jsp");
            
        }
    }

}
logins

 

添加到tomcat里面,并运行tomcat

 

 

 

 

然后浏览器输入网址:http://127.0.0.1:8080/HelloServlet/login.jsp

输入用户名  密码:  sa  123

我们试着输入一个错误用户名

OK,全部允许通过。

 

posted @ 2016-11-18 22:23  S_Wilson  阅读(920)  评论(0编辑  收藏  举报