我想手动部署和执行网络应用。
Servlet:ServletClassCalled
import java.io.IOException;
import javax.servlet.http.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.annotation.WebServlet;
import java.io.*;
import java.io.PrintWriter;
import java.util.*;
@WebServlet("/ServletClassCalled")
public class ServletClassCalled extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet (HttpServletRequest request,HttpServletResponse response) throws IOException {
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
String username = request.getParameter("username");
String chkValues[] = request.getParameterValues("chk1");
pw.write("Success");
pw.write(username);
pw.write("Chk Val 1 "+chkValues[0]+" Chk Val 2 "+chkValues[1]);
}
public void doPost (HttpServletRequest request,HttpServletResponse response) throws IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Enumeration headerNames = request.getHeaderNames();
while(headerNames.hasMoreElements()) {
String paramName = (String)headerNames.nextElement();
out.print(paramName);
String paramValue = request.getHeader(paramName);
out.println(paramValue);
}
}
}
JSP:index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<h1>Simple App</h1>
<form method="get" action="ServletClassCalled">
User Name : <input type="text" name="username" /><br/>
Hobbies: <input type="checkbox" name="chk1" value="Coding" /> Coding <input type="checkbox" name="chk1" value="Sleeping" /> Sleeping<br/>
<input type="submit" name="submit" value="submit" />
</form>
<hr/>
<form method="post" action="ServletClassCalled">
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>
部署描述符: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"
metadata-complete="true">
<!-- Define servlets that are included in the example application -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>ServletClassCalled</servlet-name>
<servlet-class>ServletClassCalled</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletClassCalled</servlet-name>
<url-pattern>/ServletClassCalled</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>GetHeader</servlet-name>
<servlet-class>GetHeader</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GetHeader</servlet-name>
<url-pattern>/GetHeader</url-pattern>
</servlet-mapping>
</web-app>
由于我在欢迎列表中列出了索引文件,因此它显示正确但当我尝试提交表单时显示错误。
答案 0 :(得分:0)
我忘了在web.xml中关闭我的标签。现在工作正常。
答案 1 :(得分:0)
对于更新的Servlet 3.0版 它提供了使用包
在内部映射servlet的功能还会生成servlet路径的自动生成代码
因此,如果您使用的是servlet 3.0,则无需在web.xml文件中编写servlet的映射