这里无法正确编译FirstServlet,而且左边也没有target目录
相关代码:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <servlet> <servlet-name>first</servlet-name> <servlet-class>com.wxb.servlet.FirstServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>first</servlet-name> <url-pattern>/hi</url-pattern> </servlet-mapping> </web-app>
相关代码:package com.wxb.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; public class FirstServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String name=req.getParameter("name"); String html = "<h1 style='color:red'>,"+name+"!</h1><hr/>"; System.out.println("返回给浏览器的相应内容为:"+html); PrintWriter out = resp.getWriter(); out.println(html); } }
浏览器访问报错:
FirstServlet未正确实例化
35
收起
正在回答 回答被采纳积分+1
1回答
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星