hi i'm trying to build a website with jetty server. i want to open jsp page as the start page but i cant make it work from there.i tried this example . i put the war file in the webapps directory and i got the first jsp page (index.jsp). but when i tried to activate the form i got this error message:
HTTP ERROR 404
Problem accessing /GrettingServlet. Reason:
NOT_FOUND
this is the files:
index.jsp
<%@ page language="java" contentType="text/html; charset=windows-1255"
pageEncoding="windows-1255"%>
<!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=windows-1255">
<title>Insert title here</title>
</head>
<body>
<form action="GrettingServlet" method="POST">
First Name: <input type="text" name="firstName" size="20"><br>
Last Name: <input type="text" name="lastName" size="20">
开发者_JAVA百科<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
GrettingServlet.java :
package main;
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;
/**
* Servlet implementation class GrettingServlet
*/
public class GrettingServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public GrettingServlet() {
super();
// 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
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String firstName = request.getParameter("firstName").toString();
String lastName = request.getParameter("lastName").toString();
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet GreetingServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<p>Welcome " + firstName + " " + lastName + "</p>");
out.println("</body>");
out.println("</html>");
out.close();
}
}
main.java:
package main;
import java.util.HashMap;
import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.bio.SocketConnector;
import org.mortbay.jetty.servlet.Context;
import org.mortbay.jetty.servlet.ServletHandler;
import org.mortbay.jetty.servlet.ServletHolder;
import org.mortbay.jetty.webapp.WebAppContext;
import org.mortbay.util.ajax.Continuation;
public class Main {
Server server;
Context root;
HelloServlet servlet;
HashMap <String, Continuation> hash;
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
new Main().start();
}
public void start() throws Exception {
hash = new HashMap<String, Continuation>();
server = new Server(80);
root = new WebAppContext(server,"C:/Users/sl300/Desktop/jetty/webapps/MyFirstServlet.war","/");
server.start();
server.join();
}
public void addContinuation(String str,Continuation cc){
hash.put(str, cc);
}
}
what is wrong? or how else i can open jsp?
Would be helpful to see your web.xml file. Seems to be a problem with the servlet mapping. Another way would be to use a ServletHandler and add it to the server instance like
GrettingServlet greetingServlet = new GrettingServlet();
ServletHandler servletHandler = new ServletHandler();
servletHandler.addServletWithMapping(greetingServlet, "/your/servlet/path");
server.addHandler(servletHandler);
精彩评论