InputServlet类
package me.example.servletdemo;
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 InputServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String info = request.getParameter("info");
response.setContentType("text/html; charset=utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println(
"<head><title>Welcome to Servlet!</title></head>");
out.println("<body>");
out.println("<h1>Hello World!</h1>");
out.println("<h1>"+info+"</h1>");
out.println("</body>");
out.println("</html>"); out.flush(); out.close(); } }
web.xml 文件
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <servlet> <servlet-name>InputServlet</servlet-name> <servlet-class>me.example.servletdemo.InputServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>InputServlet</servlet-name> <url-pattern>/InputServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>input.html</welcome-file> </welcome-file-list> </web-app>
input.html文件
<html>
<head>
<meta http-eqiv="content-Type" content="text/html; charset=utf-8">
<meta charset="utf-8"> <meta name="keywords" content="关键词 关键词">
<meta name="description" content="">
</head>
<body>
<form action="./InputServlet" method="get">
请输入内容:<input type="text" name="info">
<input type="submit" value="提交">
</form>
</body>
</html>
你直接执行了提交的动作
路径访问有两种,
1.是 根目录/文件 可以直接访问的,2.是受点保护的通过servlet来中转访问的WEB-INF下的。
3.servlet里面的处理后的页面项目的文件都可以访问的
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。