//package com.demo;
//
//import java.io.*;
//import javax.servlet.*;
//import javax.servlet.http.*;
//import javax.servlet.annotation.WebServlet;
//import com.demo.Student;
//
//@WebServlet("/firstservlet")
//public class FirstServlet extends HttpServlet {
//    private ServletContext context; 
//    
//    @Override
//    public void init(ServletConfig config) throws ServletException {
//        super.init(config);
//        context = getServletContext();
//    }
//    
//    @Override
//    protected void doGet(HttpServletRequest request, HttpServletResponse response)
//            throws ServletException, IOException {
//        doPost(request, response);
//    }
//    
//    @Override
//    protected void doPost(HttpServletRequest request, HttpServletResponse response)
//            throws ServletException, IOException {
//        // 设置编码
//        request.setCharacterEncoding("UTF-8");
//        response.setCharacterEncoding("UTF-8");
//        response.setContentType("text/html;charset=UTF-8");
//        
//        // 获取参数并进行空值检查
//        String snumber = request.getParameter("snumber");
//        String sname = request.getParameter("sname");
//        
//        if (snumber != null && sname != null) {
//            // 转换编码
//            snumber = new String(snumber.getBytes("UTF-8"), "UTF-8");
//            sname = new String(sname.getBytes("UTF-8"), "UTF-8");
//            
//            // 创建学生对象
//            Student stud = new Student(snumber, sname);
//            request.setAttribute("stud", stud);
//            
//            // 转发请求
//            RequestDispatcher view = request.getRequestDispatcher("/secondservlet");
//            view.forward(request, response);
//        } else {
//            // 参数为空时的处理
//            response.getWriter().println("错误：学号或姓名不能为空！");
//        }
//    }
//}


package com.demo;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;

@WebServlet("/FirstServlet")
public class FirstServlet extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        // 获取表单参数
        String sno = request.getParameter("sno");
        String name = request.getParameter("name");
        
        // 创建Student对象
        Student student = new Student(sno, name);
        
        // 将Student对象存储在ServletContext中
        ServletContext context = getServletContext();
        context.setAttribute("student", student);
        
        // 转发请求到SecondServlet
        RequestDispatcher dispatcher = context.getRequestDispatcher("/SecondServlet");
        dispatcher.forward(request, response);
    }
}

