Eclipse运行第三个程序之YoungerAndOlder

实验内容

  • 1.使用servlet接收前台表单数据,表单包括用户姓名、年龄、所在学校
  • 2.用get和post两种方式传送到后台,用户姓名不允许为空
  • 3.servlet1接收到数据以后,调用servlet2进行验证,如果验证年龄小于20,返回数据“欢迎你,少年”(利用forward和include来操作)
  • 4.如果年龄大于40岁,页面跳转至一个jsp(使用send redirect ),显示 “欢迎您,老先生”

文章目录

  • 1.整体项目结构
  • 2. 代码
    • 2.1 填写信息页面---index.jsp
    • 2.2 servlet1.java
    • 2.3 servlet2.java
    • 2.4 info.jsp
  • 3. 运行
  • 4.注意点

1.整体项目结构

Eclipse运行第三个程序之YoungerAndOlder_第1张图片

2. 代码

2.1 填写信息页面—index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title heretitle>

<script>
	function testUserName()
    {
       
         var userName=document.formUser.userName.value;
      
         if(userName=="")
         {
       
        	alert("用户姓名不能为空!");
 	        return false;
         }
         
         return true;
    }
script>

head>
<body>
	<form action="/demo5YoungerAndOlder/servlet1" method="get" onsubmit="return testUserName()" name="formUser">
		用户姓名:<input type="text"  name="userName" id="userName" /><br />
		年龄:<input type="text" name="userAge" id="userAge"/><br />
		所在学校: <input type="text" name="userSchool" id="userSchool"/><br />			        		
		<input type="submit" value="提交"/>   
	form>
body>
html>

2.2 servlet1.java

package com.szh.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class servlet1
 */
@WebServlet("/servlet1")
public class servlet1 extends HttpServlet {
     
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public servlet1() {
     
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     
		System.out.println("get请求将执行");
		
		String userName = request.getParameter("userName");
		//String userName = new String(request.getParameter("userName").getBytes("ISO-8859-1"),"utf-8");
		String userAge = request.getParameter("userAge");
		@SuppressWarnings("unused")
		String userSchool = request.getParameter("userSchool");
		
		response.setContentType("text/html;charset=utf-8");
		request.setAttribute("userName",userName );
		request.setAttribute("userAge",userAge );
		
		
		request.getRequestDispatcher("/servlet2").forward(request,response);//不是重定向,而是转发
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     
		System.out.println("post请求将执行");
		doGet(request, response);
	}

}

2.3 servlet2.java

package com.szh.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class servlet2
 */
@WebServlet("/servlet2")
public class servlet2 extends HttpServlet {
     
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public servlet2() {
     
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     
		String userName = (String) request.getAttribute("userName");
		String userAge = (String) request.getAttribute("userAge");
		
		
		int age = Integer.parseInt(userAge);
		System.out.println(age);
		
		if(age < 20) 
		{
     	
			PrintWriter out = response.getWriter();
			out.print("欢迎你 ,少年"); 
		}
		if(age > 40)
		{
     
			request.getSession().setAttribute("name",userName );
			response.sendRedirect("/demo5YoungerAndOlder/info.jsp");//重定向
		}
	}
	

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

2.4 info.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>欢迎您,老先生title>
head>
<body>
欢迎您,老先生<h1><%=request.getSession().getAttribute("name") %>h1>
body>
html>




3. 运行

Eclipse运行第三个程序之YoungerAndOlder_第2张图片
Eclipse运行第三个程序之YoungerAndOlder_第3张图片




4.注意点

代码是正确的,唯一注意的地方是路径

你可能感兴趣的:(#,eclipse)