javaweb 基於java Servlet登入 簡單入門案例

來源:互聯網
上載者:User

標籤:列印   ted   輸入中文   server   五步   post   沒有   項目   word   

項目流程

第一步:建立一個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,全部允許通過。

 

javaweb 基於java Servlet登入 簡單入門案例

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.