赞
踩
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!--全局初始化参数=全局变量--> <context-param> <param-name>allparam</param-name> <param-value>allparam参数</param-value> </context-param> <servlet> <servlet-name>b1</servlet-name> <servlet-class> //这里的web是src文件下web包里的UserServlet web.UsersServlet </servlet-class> <init-param> <!--针对某一个servle=局部变量--> <param-name>myparam</param-name> <param-value>myparam的值</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>b1</servlet-name> <url-pattern>/login</url-pattern> </servlet-mapping> <!--配置监听器--> <listener> <listener-class>util.SessionListener</listener-class> </listener> <listener> <listener-class>util.AttributeListener</listener-class> </listener> </web-app>
package web; import bean.LoginInfo; import dao.LoginInfoDao; import dao.impl.LoginInfoDaoimpl; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.*; import java.io.IOException; import java.io.PrintWriter; public class UsersServlet extends HttpServlet { LoginInfoDao loginInfoDao = null; @Override public void init(ServletConfig config) throws ServletException { //这里可以访问web.xml里配置的全局初始化参数和局部初始化参数 //局部初始化参数 System.out.println("UsersServlet-init->myparam"+ config.getInitParameter("myparam")); //全局初始化参数 System.out.println("UsersServlet-init->allparam"+ config.getInitParameter("allparam")); } public UsersServlet() { loginInfoDao = new LoginInfoDaoimpl(); int success = loginInfoDao.createUserInfoTable(); success = loginInfoDao.addUserInfo(1,"admin","123"); } @Override public void destroy() { System.out.println("UsersServlet销毁"); } @Override public void init() throws ServletException { System.out.println("UsersServlet初始化"); } @Override //接收get请求:超链接 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //接收用户传递过来的参数 String uage = req.getParameter("uage"); String upass = req.getParameter("upass"); System.out.println(uage+","+upass); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //处理post请求的乱码 req.setCharacterEncoding("utf-8"); //接收用户传递过来的参数 //无论前台传递什么格式的数据,后台都是使用String接收的 String username = req.getParameter("username"); String password = req.getParameter("password"); String quit = req.getParameter("quit"); HttpSession session= req.getSession(); if(quit !=null && quit.equals("退出")){ session.removeAttribute("username"); session.removeAttribute("password"); req.getRequestDispatcher("index.jsp").forward(req,resp); return; } Boolean success = loginInfoDao.queryUserInfo(username, password, LoginInfo.class); if(success){ //登录成功 session.setAttribute("username",username); session.setAttribute("password",password); System.out.println("帐号:"+ username); System.out.println("密码:"+ password); req.getRequestDispatcher("/success.jsp").forward(req,resp); }else { //登陆失败 //1.后台创建cookie Cookie cookie = new Cookie("uname",username); //返回给前端 resp.addCookie(cookie); resp.sendRedirect("index.jsp"); } } }
//action里的login需要配置WEB_INF里的web.xml来进行调用
//当提交表单时,login在web.xml里绑定了mapping里的b1,
//b1找到servlet里的b1,再找到servlet-class,实例化UserServlet类
<form action="login" method="post">
用户名:<input type="text" name="username" value="${unamecookie}" required>
密码:<input type="password" name="password" required>
<input type="submit" name="login" value="登录">
<br>
<h1>帐号:admin,密码:123,数据库:javaweb,表:login</h1>
</form>
<%@ page contentType="text/html;charset=UTF-8" language="java" errorPage="test2.jsp" %> <html> <head> <title>Title</title> </head> <body> <% //某一次请求 request.setAttribute("a1","aaa"); %> request=<%= request.getAttribute("a1")%><br> <% //response.sendRedirect(" "); response.getWriter(); %> <% //整个会话期间 session.setAttribute("b1","bbb"); %> session=<%=session.getAttribute("b1")%><br> <% //application表示的是整个(服务器)程序运行期间 application.setAttribute("c1","ccc"); %> application=<%=application.getAttribute("c1")%><br> <% //page = this %> <% //当前页面 pageContext.setAttribute("d1","ddd"); %> pageContext=<%=pageContext.getAttribute("d1")%> <a href="test2.jsp">test2.jsp</a> <%=6/0%> </body> </html>
<%@ page import="java.util.ArrayList" %> <%@ page import="java.util.List" %> <%@ page contentType="text/html;charset=UTF-8" language="java" isErrorPage="true" %> <html> <head> <title>Title</title> </head> <body> <% List list = new ArrayList(); %> <h1>test2.jsp</h1> request=<%= request.getAttribute("a1")%><br> session=<%=session.getAttribute("b1")%><br> application=<%=application.getAttribute("c1")%><br> pageContext=<%=pageContext.getAttribute("d1")%> <% out.print("out---"); %> <% out.print("<script>alert('success');</script>"); %> <% config.getInitParameter("");%> exception=<%= exception.getMessage() %> </body> </html>
<%@ page import="java.util.ArrayList" %> <%@ page import="java.util.List" %> <%@ page import="java.util.HashMap" %> <%@ page import="java.util.Map" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h1>top.jsp</h1> <% //EL表达式如果想要读取变量,则该变量一定要在作用域中:pageContext,request,session,application int a=10; pageContext.setAttribute("a1",a); List list = new ArrayList(); list.add("abc"); pageContext.setAttribute("a2",list); Map map = new HashMap(); map.put("k1","key"); pageContext.setAttribute("a3",map); //如果在相同的key的前提下,不同的作用域,el表达式会选择展示哪个值 pageContext.setAttribute("a4","pageContext内容"); request.setAttribute("a4","request内容"); session.setAttribute("a4","session内容"); application.setAttribute("a4","application内容"); %> ${20+30}<br> ${20>30}<br> ${20<30}<br> ${20>30?"aaa":"bbb"}<br> a=${a1}<br> abc=${a2[0]}<br> k1=${a3.k1}<br> a4=${a4}<br> pageContext-a4=${pageScope.a4}<br> request-a4=${requestScope.a4}<br> session-a4=${sessionScope.a4}<br> application-a4=${applicationScope.a4}<br> </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%@include file="top.jsp"%>
</body>
</html>
<%@ page import="java.util.List" %> <%@ page import="java.util.ArrayList" %> <%@ page import="java.util.Date" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <html> <head> <title>Title</title> </head> <body> <h1>jstl</h1> <c:set var="uname" value="张三" scope="session"></c:set> <c:out value="${sessionScope.uname}"></c:out> <c:if test="${uname=='张三'}"> 这是张三 </c:if> <c:choose> <c:when test="${uname=='张三'}"> c-choose-张三 </c:when> <c:otherwise> c-choose-不是张三 </c:otherwise> </c:choose> <% List list = new ArrayList(); list.add("abc"); list.add("bcd"); list.add("1李四"); pageContext.setAttribute("ulist",list); %> <h4>循环数据</h4> <c:forEach items="${ulist}" var="user" varStatus="sta"> ${sta.count}-${sta.index}---> ${user}<br> </c:forEach> <% pageContext.setAttribute("mytime",new Date()); %> date=<fmt:formatDate value="${mytime}" pattern="yyyy-MM-dd"></fmt:formatDate> </body> </html>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。