2013-06-21 7 views

risposta

10

Un semplice esempio è:

@WebServlet(value="/hello") 
public class HelloServlet extends HttpServlet { 

    @Override 
    public void doGet(HttpServletRequest request,HttpServletResponse response) 
     throws ServletException, IOException { 
    PrintWriter out = response.getWriter(); 

    // then write the data of the response 
    String username = request.getParameter("username"); 
    if (username != null && username.length() > 0) { 
     out.println("<h2>Hello, " + username + "!</h2>"); 
     } 
    } 

} 
+1

Sì, è giusto, ho trovato la soluzione dopo averla postata. – Sheel

5

Annotation rappresenta i metadati. Se si utilizza l'annotazione, non è richiesto il descrittore di distribuzione (file web.xml). Ma dovresti avere tomcat7 perché non funzionerà nelle versioni precedenti di tomcat. L'annotazione @WebServlet viene utilizzata per mappare il servlet con il nome specificato.

@WebServlet("/Simple") 
public class Simple extends HttpServlet { 
    private static final long serialVersionUID = 1L; 

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 


     response.setContentType("text/html"); 
     PrintWriter out=response.getWriter(); 

     out.print("<html><body>"); 
     out.print("<h3>Hello Servlet</h3>"); 
     out.print("</body></html>"); 
    } 

} 
+1

Ho già trovato una soluzione, se vuoi rispondere, ho postato una domanda per controllarla. – Sheel

Problemi correlati