所以我创建了新的maven模块。我使用" maven-archetype-webapp"创造它。当我在index.jsp中执行普通文本时,它可以正常工作。但我也想做servlet。
我将很快写下我的装箱方法。我点击了我的Module和New-> Servlet。 Java包:pl.marek,类名:MyServlet以及后来我选择" doPost"和" doGet"。以下是我的文件的样子:
的index.jsp
<%@page import="java.util.Date"%>
<%@page import="pl.marek.countriesmapper.reader.*" %>
<%@page import="pl.marek.countriesmapper.preparer.*" %>
<%@page import="pl.marek.countriesmapper.country.*" %>
<%@ page language="java" contentType="text/html; charset=US-ASCII"
pageEncoding="US-ASCII"%>
<!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=US-ASCII">
<title>Hello</title>
</head>
<body>
<form action="MyServlet">
Please enter your Username: <input type="text" name="username" size="20px"> <br>
Please enter your Password: <input type="text" name="password" size="20px"> <br><br>
<input type="submit" value="submit">
</form>
</body>
</html>
MyServlet.java
package pl.marek;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class MyServlet
*/
public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public MyServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
PrintWriter out = response.getWriter();
out.println (
"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" +" +
"http://www.w3.org/TR/html4/loose.dtd\">\n" +
"<html> \n" +
"<head> \n" +
"<meta http-equiv=\"Content-Type\" content=\"text/html; " +
"charset=ISO-8859-1\"> \n" +
"<title> Crunchify.com JSP Servlet Example </title> \n" +
"</head> \n" +
"<body> <div align='center'> \n" +
"<style= \"font-size=\"12px\" color='black'\"" + "\">" +
"Username: " + username + " <br> " +
"Password: " + password +
"</font></body> \n" +
"</html>"
);
}
}
/**
* @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);
}
}
Web.xml中
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>MyServlet</servlet-name>
<display-name>MyServlet</display-name>
<description></description>
<servlet-class>pl.marek.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>MyServlet</url-pattern>
</servlet-mapping>
</web-app>
这是我项目的照片:
当我点击提交时,我有地址:localhost:8080 / WebAplication / MyServlet?username = test123&amp; password = test321
但在网页上我有:
HTTP Status 404 -
type Status report
message
description The requested resource is not available.
Apache Tomcat/7.0.47
有人可能知道我做错了什么? :/
@edit
我删除了&#34; /&#34;在web.xml中,现在我有了这个:
HTTP Status 500 - Error instantiating servlet class pl.marek.MyServlet
错误500已解决,但我仍然有状态404:/
@ EDIT2
我做了什么写了SaviNuclear,我仍然有404错误:/
@ EDIT3
问题解决了。我不得不创建src / java而不是src / resources。