输入地址(http://localhost:8088/hi?name=tom)404?
相关代码:
1、配置依赖
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
</dependencies>
2、书写servlet类
package com.imooc.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FirstServlet extends HttpServlet {
public FirstServlet() {
}
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
String name = req.getParameter("name");
String html = "<h1 style='color:red'>h1," + name + "!</h1><hr/>";
System.out.println("返回给浏览器的响应内容为:" + html);
PrintWriter out = resp.getWriter();
out.println(html);
}
}
3、映射servlet
<servlet>
<servlet-name>first</servlet-name>
<servlet-class>com.imooc.servlet.FirstServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>first</servlet-name>
<url-pattern>/hi</url-pattern>
</servlet-mapping>
结果
相关截图:
但是http://localhost:8088/first/index.html可以访问
46
收起
正在回答 回答被采纳积分+1
1回答
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星