index里面
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="Helloservlet">你好</a>
<form action="/Helloservlet" method="post">
<input type="submit" value="很好"/>
</form>
</body>
</html>
Helloservlet里面的代码如下,没有配置web.xml因为已经有了注解
package servlet;
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;
/**
* Servlet implementation class Helloservlet
*/
@WebServlet("/Helloservlet")
public class Helloservlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public Helloservlet() {
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request, response);
response.getWriter().append("Served at: ").append(request.getContextPath());
System.out.println("很好");
PrintWriter out=response.getWriter();
response.setContentType("text/html;charset=utf-8");
out.println("<strong>xixi</strong>");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
System.out.println("很好");
PrintWriter out=response.getWriter();
response.setContentType("text/html;charset=utf-8");
out.println("<strong>xixi</strong>");
}
}
但是一直出现以下问题,已经一个消失了都没有解决成功,求大神来解答
我的是xml文件中<url-pattern>/Servlet/HelloServlet</url-pattern>的Servlet首字母是小写,改成大写就OK了
路径出错,其实你应该直接访问页面,而不是访问servlet