加入C标签:
加入jstl.jar 和standard.jar加入Lib文件夹中
将c.tld放入WEB-Info文件夹中
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <style> #div { position:absolute; left:20%; top:10%; } </style> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <div id="div"> <form action="Login" method="post"> 名字:<input type="text" name="uname" id="uname"/> <br> 密码:<input type="password" name="upwd" id="upwd"/> <br> 请选择自己喜欢的颜色: <br> <input type="radio" name="color" value="红色" />红色 <input type="radio" name="color" value="绿色" checked />绿色 <input type="radio" name="color" value="绿色" />蓝色 <br> 选择喜欢的运动: <br> <input type="checkbox" name="checkbox" value="篮球"/>篮球 <input type="checkbox" name="checkbox" value="足球"/>足球 <input type="checkbox" name="checkbox" value="乒乓球"/>乒乓球 <br> 选择需要睡觉的时间:<select name="utime"> <option value="7小时">7小时</option> <option value="8小时">8小时</option> <option value="9小时">9小时</option> </select> <br> 个人简介: <br> <textarea name="ps" id="ps" rows="10" cols="30"></textarea> <br> <input type="submit" /> <input type="reset"/> </form> </div> </body> </html>welcome.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!-- 加入JSTL标签--> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'welcome.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> 你的用户名是: ${uname} <!-- EL表达式 --> 密码是: ${upwd} <br> 你喜欢的颜色是:${color} <br> 你喜欢的运动是: <c:forEach items="${ch}" var="ch"> <!-- C标签 --> ${ch} </c:forEach> <br> 需要睡觉的时间 ${utime} <br> 个人简介 ${ps} </body> </html>新建一个servlet包,建一个Login.java文件
package servlet; import java.io.IOException; import java.io.UnsupportedEncodingException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Login extends HttpServlet{ public void doGet(HttpServletRequest request,HttpServletResponse response) { response.setCharacterEncoding("UTF-8"); try { request.setCharacterEncoding("UTF-8"); } catch (UnsupportedEncodingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } String uname=request.getParameter("uname");//获取数据 request.setAttribute("uname", uname); //设值 String upwd=request.getParameter("upwd"); request.setAttribute("upwd", upwd); String color=request.getParameter("color"); request.setAttribute("color", color); String ps=request.getParameter("ps"); request.setAttribute("ps", ps); String utime=request.getParameter("utime"); request.setAttribute("utime", utime); String checkbox[]=request.getParameterValues("checkbox"); request.setAttribute("ch", checkbox); if(uname.equals("admin")&&upwd.equals("123")) { try { request.getRequestDispatcher("welcome.jsp").forward(request, response); } catch (ServletException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else try { response.sendRedirect("index.jsp"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void doPost(HttpServletRequest request,HttpServletResponse response) { this.doGet(request, response); } public void destory() { super.destroy(); } }
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <servlet> <servlet-name>Login</servlet-name> <servlet-class>servlet.Login</servlet-class> </servlet> <servlet-mapping> <servlet-name>Login</servlet-name> <url-pattern>/Login</url-pattern> </servlet-mapping> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>