servlet从jsp到jsp实现例子

已登录页面为例子:

1.login.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>

2.在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_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>

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

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);
       
       
    }
}

4.结果页面

<%@ 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>

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

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

servlet从jsp到jsp实现例子,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。