servlet从jsp到jsp实现例子

已登录页面为例子:

1.login.jsp

servlet从jsp到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>
</head>
<body>
<form action="login">
    username:<input  type="text" name="username" /><br>
    password:<input type="password" name="password" ><br>
    repassword:<input type="password" name="repassword"><br>
    age:<input type="text" name="age"/>
    <input type="submit" value="submits">
</form>

</body>
</html>
servlet从jsp到jsp实现例子

2.在xml中配置好传输信息

servlet从jsp到jsp实现例子
 <?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">
 
  <servlet>
      <servlet-name>Login</servlet-name>
      <servlet-class>com.servlet.LoginServlet</servlet-class>
  </servlet>
  
  <servlet-mapping>
      <servlet-name>Login</servlet-name>
      <url-pattern>/login</url-pattern>
  </servlet-mapping>

</web-app>
servlet从jsp到jsp实现例子

3.通过xml配置信息找到对应的处理class类。

servlet从jsp到jsp实现例子
package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginServlet extends HttpServlet
{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException
    {
       String username = req.getParameter("username");
       String password = req.getParameter("password");
       String repassword = req.getParameter("repassword");
       int age = Integer.parseInt(req.getParameter("age"));
       
       String result = "";
       if(age > 18)
       {
           result = "success";
       }
       else
       {
           result += "age <= 18";
       }
       if(!password.equals(repassword))
       {
           result += "password != repassword";
       }
       
       req.setAttribute("username", username);
       req.setAttribute("age", age);
       //转发
       RequestDispatcher rd = req.getRequestDispatcher("result.jsp");
       
       //遍历一个list。额外加上的。测试request后得到的是一个枚举类型数据
       List<String> list = new ArrayList<String>();
       for(int i = 0; i < 100; i++)
       {
           list.add(String.valueOf(i));
       }
       req.setAttribute("list", list);
       
       
    }
}
servlet从jsp到jsp实现例子

4.结果页面

servlet从jsp到jsp实现例子
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.List" %>
<!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>
</head>
<body>

username:<%= request.getAttribute("username") %>

age:<%= request.getAttribute("age") %>

<%
    List<String> list = (List<String>)request.getAttribute("list");
    for(int i = 0; i < list.size(); i++)
    {
        %>
        <%= list.get(i) %><br>
        <%
    }

%>
</body>
</html>
servlet从jsp到jsp实现例子

完成,收工,其中有些变量没有得到值,是因为没有将其存入到request容器中。

只是个例子,如果需要,留下言,我可以将其他的补全。

servlet从jsp到jsp实现例子,布布扣,bubuko.com

servlet从jsp到jsp实现例子

上一篇:MVC里使用JSON方法集锦


下一篇:Extjs 4 如何动态加载 Controller