在前一个的基础之上写一个用户信息登记,jsp,servlet转发,filter处理字符


我是在webcontent-login-下建了index.jsp,manager.jsp 

在com.atguigu.login包中建立AddAServlet.java和User.java

注意点:

1/不是在同一个包或者文件中,所以在manager.jsp中我们用到User的方法什么的,要在head中引入<%@ page import="com.atguigu.login.User,java.util.List" %>

2/在一个项目的web.xml中可写多个servlet ,filter  通过action到哪个servlet,和servet的class 格外注意

3/ServletContext application=getServletContext();
    application.setAttribute("users", user);

    传说中的servlet 上下文servket和jsp中传递信息

index.jsp

<body>
  <form action="<%=request.getContextPath() %>/AddAServlet" method="post">
  <table align="center" width="330">
    <tr> <td align="center" colspan="2">
      <b>  添加用户</b></td>
     </tr>
     <tr> <td>name:</td><td><input type="text" name="name"/></td>
     </tr>
     <tr> <td>address:</td><td><input type="text" name="add"/></td>
     </tr>
    <tr> <td align="center" colspan="2"><input type="submit" value="submit"/></td>
    </tr>
</body>



AddAServlet .java

public class AddAServlet extends HttpServlet {
private static final long serialVersionUID = 1L;



protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name=request.getParameter("name");
String add=request.getParameter("add");
User user=new User();
user.setName(name);
user.setAddress(add);
ServletContext application=getServletContext();

application.setAttribute("users", user);
request.getRequestDispatcher("/login/manager.jsp").forward(request, response);;

}


}

web.xml

<?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_3_0.xsd" version="3.0">
  <display-name>Charactor</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>
  <filter>
    <filter-name>CharactorFilter</filter-name>
    <filter-class>com.atguigu.charactor.CharactorFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>CharactorFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>


 
 


 <servlet>
    <servlet-name>AddServlet</servlet-name>
    <servlet-class>com.atguigu.charactor.AddServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>AddServlet</servlet-name>
    <url-pattern>/AddServlet</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>AddAServlet</servlet-name>
    <servlet-class>com.atguigu.login.AddAServlet</servlet-class>
   </servlet>
   <servlet-mapping>
   <servlet-name>AddAServlet</servlet-name>
   <url-pattern>/AddAServlet</url-pattern>
   </servlet-mapping>
   
    
</web-app>


User.java

package com.atguigu.login;


public class User {
 private String name;
 private String address;


 
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
 
}


manager.jsp

<%@ 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>Insert title here</title>
<%@ page import="com.atguigu.login.User,java.util.List" %>
</head>
<body>
    <%
       User users=(User)application.getAttribute("users");
    %>
   <%=users.getName() %>
   <%=users.getAddress() %>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值