时间:2023-09-21 09:12:01 | 来源:网站运营
时间:2023-09-21 09:12:01 来源:网站运营
大数据从入门到深入:JavaEE 之 动态网页开发基础 JSP实战优化:Servlet基础:<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>用户登录</title></head><body> <div> <h1>servlet小案例:用户登录</h1> <form action="userlogin" method="post"> <p> 请输入您的用户名 :<input type="text" name="loginName" /> </p> <p> 请输入您的密码 : <input type="password" name="loginPass" /> </p> <p> <button type="submit">登录</button> </p> </form> </div></body></html>
登录成功: <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>登录成功</title></head><body> <div> <marquee> <h1>恭喜,登录成功!</h1> </marquee> </div></body></html>
登录失败:<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>登录失败</title></head><body> <div> <marquee> <h1>很遗憾,登录失败!</h1> </marquee> </div></body></html>
2) 创建Servlet package com.hnxy.controller;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * Servlet implementation class UserLoginAction11 */public class UserLoginAction extends HttpServlet { private static final long serialVersionUID = 1L; /** * @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()); } /** * @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); }}
稍加修改就可以使用了package com.hnxy.controller;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * 用户登录的控制器 * @author My * */public class UserLoginAction extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 设定编码 request.setCharacterEncoding("UTF-8"); // 获取页面数据 String loginName = request.getParameter("loginName"); String loginPass = request.getParameter("loginPass"); // 业务判断 if("admin".equals(loginName)&&"admin".equals(loginPass)){ request.getRequestDispatcher("success.jsp").forward(request, response); }else{ response.sendRedirect("error.jsp"); } }}
3) 调试运行 <?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>javaweb_class3_tp1</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <description></description> <display-name>UserLoginAction</display-name> <servlet-name>UserLoginAction</servlet-name> <servlet-class>com.hnxy.controller.UserLoginAction</servlet-class> </servlet> <servlet-mapping> <servlet-name>UserLoginAction</servlet-name> <url-pattern>/userlogin</url-pattern> </servlet-mapping></web-app>
简单了解一下什么是XML? <!-- webapp的项目名称 --><display-name>javaweb_class3_tp1</display-name>
<!-- webapp的欢迎页面 也就是说如果访问 http://127.0.0.1:8080/javaweb_class3_tp1 没有指定要访问的JSP的话 tomcat会默认跳转到下面存在的一个页面中去 --> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file></welcome-file-list>
上面的都是不重要的 下面才是重要的<servlet> <description></description> <display-name>UserLoginAction</display-name> <servlet-name>UserLoginAction</servlet-name> <servlet-class>com.hnxy.controller.UserLoginAction</servlet-class> </servlet> <servlet-mapping> <servlet-name>UserLoginAction</servlet-name> <url-pattern>/userlogin</url-pattern> </servlet-mapping>
这个是我们UserLoginAction这个Servlet的核心配置部分,不知道大家还有没有印象刚才我们 通过页面请求已经分析到 <servlet> <description></description> <display-name>UserLoginAction</display-name> <servlet-name>UserLoginAction</servlet-name> <servlet-class>com.hnxy.controller.UserLoginAction</servlet-class> </servlet> <servlet-mapping> <servlet-name>UserLoginAction</servlet-name> <url-pattern>/userlogin</url-pattern> </servlet-mapping>
每个servlet都有一个这个的配置,需要注意的是: 关键词:基础,实战,入门,数据,深入,动态