ホームページ  >  記事  >  Java  >  Java を使用して Web ページのログインと登録を実装する

Java を使用して Web ページのログインと登録を実装する

一个新手
一个新手オリジナル
2017-09-07 09:26:206107ブラウズ

1. Web ページの作成には HTML、JSP、サーブレットがありますが、これら 3 つはそれぞれ長所と短所があります。HTML は一部の静的な表示の作成に適しており、JSP は動的および変数の表示の作成に適しています。ビジネス ロジック、分散ステアリング、DAO データ転送の処理に使用されます。

2. このプロジェクトは、jsp+サーブレットを使用して簡単な Web ページの登録とログインを実装します。これには、98a199e2b434149b4b49cce37a5985d8 などのいくつかの基本的な JSP 構文が必要です。5b80f63fa6b362342da217a1bc06c7f85db79b134e9f6b82c0b36e0489ee08ed表示转发。form跟html一样表示表单。117befdeba1fd957497e82f8445cb3d1 はコメントを示します。
a51d2e2da6e7069c0f0d8111919fc496 グローバル変数の宣言などの基本的な構文と、taglib

3. Web ページを作成し、デフォルトで web.xml ファイルを生成します。プロジェクト名は webServletTest で、java Resources ディレクトリに webservlet.java を作成します。

package webJspDemo.com;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/**
 * Servlet implementation class LoginServlet
 */@WebServlet("/servlet/loginservlet")public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {        super();        // TODO Auto-generated constructor stub
    }    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        // TODO Auto-generated method stub
        response.getWriter().append("Served at: ").append(request.getContextPath());        //获取表单数据
        response.setContentType("text/html; charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        String userName = request.getParameter("userName");
        String pwd = request.getParameter("pwd");        if("tom".equals(userName)&&"123".equals(pwd)){
            request.getSession().setAttribute("name", userName);
            request.getRequestDispatcher("/register.jsp").forward(request, response);
        }else{
            request.setAttribute("msg", "用户名或者密码不正确");
            request.getRequestDispatcher("/login.jsp").forward(request, response);
        }        //分发转向
    }    /**
     * @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);
    }

}

4. web.xml のマッピング関係とクラス名を変更します。

  <servlet>
    <servlet-name>LoginServlet</servlet-name>
    <servlet-class>webJspDemo.com.LoginServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <url-pattern>/login</url-pattern>
  </servlet-mapping>

5. WEB-INF ディレクトリに 3 つの jsp ファイル (home.jsp、login.jsp、register.jsp ファイル) を作成します。

home.jsp コードは

<body>
    <h1>欢迎来到本站!</h1>
    <%        
    String userName =(String)session.getAttribute("name");
    out.print(userName);
    %>
</body>

login.jsp コードは:

<body>
    <%        
    String msg = (String)request.getAttribute("msg");      
    if(msg !=null){
          out.print(msg);     
      }

    %>
    <form action="/webJspDemo1/servlet/loginservlet" method="get">
        用户名:<input type="text" name="userName"/><br/>
        密码:<input type="password" name="pwd"/><br/>
        <input type="submit" value="登录"/><br/>

    </form></body>

register.jsp コードは:

<body>
    欢迎你登录:    <% 
        String userName =(String)session.getAttribute("name");
        out.print(userName);        
    %>
    <a href="/webJspDemo1/home.jsp">跳到主页 </a></body>

5. 実験結果
ブラウザに http://localhost:8080/webJspDemo1/login と入力します。 フォームに tom と 123 を入力すると、register.jsp インターフェイスにジャンプし、次に home.jsp インターフェイスにジャンプし、基本的には簡単な登録プロセスが完了します。


以上がJava を使用して Web ページのログインと登録を実装するの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。