package com.demo;

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;

@WebServlet("/HelloUserServlet2")
public class HelloUserServlet2 extends HttpServlet {
    private static final long serialVersionUID = 1L;
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
            
        // 获取请求参数
        String userName = request.getParameter("userName");
        if (userName == null || userName.trim().isEmpty()) {
            userName = "Guest";
        }
        
        // 设置响应内容类型
        response.setContentType("text/html;charset=UTF-8");
        
        try (PrintWriter out = response.getWriter()) {
            // 输出HTML内容
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Hello User</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h3>Hello, " + userName + "</h3>");
            out.println("</body>");
            out.println("</html>");
        }
    }
}
