1.写网页有html,jsp,servlet.但是三者有各自的优缺点,html适合写一些静态的显示,jsp适合写动态可变的显示,servlet适合处理业务逻辑,分发转向,DAO数据中转。
2.本项目使用jsp+servlet来实现一个简单的网页注册和登录,需要一点jsp的语法基础,比如<% 插入java代码%>、<a herf=""></a>
表示转发。form跟html一样表示表单。<!---- ----->
表示注释。
<%! %>申明全局变量等基础的语法,还有几个指令page include taglib
3.建立一个web网页,默认生成一个web.xml文件。工程名为webServletTest 在java Resouces目录下建立一个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를 사용하여 웹페이지 로그인 및 등록 구현의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!