ここでは、セッションに基づく Java Web の登録方法を例に挙げて説明します。具体的には次のとおりです。
パッケージ cn.com.login;import java.io.IOException;import java.io.PrintWriter;import java.util.ArrayList;import java.util.List;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet ;インポート javax.servlet.http.HttpServletRequest;インポートjavax.servlet.http.HttpServletResponse;public class Login extends HttpServlet { private staticfinallongserialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8");文字列 userName=request.getParameter("userName");文字列パスワード=request.getParameter("パスワード"); PrintWriter out=response.getWriter(); List<ユーザー> list=Db.getAll(); for(ユーザー user:list) { if(user.getUserName().equals(userName)&&user.getPassword().equals(password)) { request.getSession().setAttribute("user", user); response.sendRedirect("/Session/index.jsp");戻る ; out.write("用户名または者密码错误!"); protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } }}class Db{ public static List<User> list=new ArrayList(); static { list.add(new User("aaa","123")); list.add(新しいユーザー("bbb","123")); list.add(新しいユーザー("ccc","123")); public static List<User> getAll() { リストを返します。 }}package cn.com.login;public class User {private String userName;プライベート文字列パスワード。 public User() { super(); // TODO 自動生成されたコンストラクター スタブ } public User(String userName, String password) { super(); this.userName = ユーザー名; this.password = パスワード; public String getUserName() { ユーザー名を返します。 public void setUserName(String userName) { this.userName = userName; } public String getPassword() { パスワードを返します。 public void setPassword(String パスワード) { this.password = パスワード; }}パッケージ cn.com.login;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession;/** *サーブレットの実装 class LogOut */public class LogOut extends HttpServlet { private staticfinallongserialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session=request.getSession(false); if(session==null) {response.sendRedirect("/Session/index.jsp");戻る ; session.removeAttribute("ユーザー"); response.sendRedirect("/Session/index.jsp"); protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } }}<!DOCTYPE html><html> <head> <title>Index.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description " content="これは私のページです"> <meta http-equiv="content-type" content="text/html"> <!--<link rel="stylesheet"; type="text/css" href="./styles.css">--> </head> <body> <form action="/Session/Login"> 用户名:<input type="text" name= "userName"/><br/> 秘密:<input type="password" name="password"/><br/> <input type="submit" value="登录" name="login"/> </フォーム> </body></html>ここで説明した内容が、大規模な Java Web プログラムの設計に役立つことを願っています。