Servlets和Spring集成

时间:2013-05-08 16:11:24

标签: spring jsp servlets

我是JSP和Servlets的新手,

web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">

    <servlet>
        <servlet-name>LoginPage</servlet-name>
        <servlet-class>com.planner.servlet.LoginPage</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>LoginPage</servlet-name>
        <url-pattern>/LoginPage</url-pattern>
    </servlet-mapping>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContextBL.xml</param-value>
    </context-param>
</web-app>

index.jsp文件

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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>
    <form method="POST" action="/LoginPage">
        <table border="1">
            <tr>
                <td>Login</td>
                <td><input type="text" name="login" />
                </td>
            </tr>
            <tr>
                <td>Senha:</td>
                <td><input type="password" name="pass" /></td>
            </tr>
            <tr>
                <input type="submit" value="Entrar" />
            </tr>
        </table>
    </form>
</body>
</html>

LoginPage.java

package com.planner.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;

import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * Servlet implementation class LoginPage
 */
public class LoginPage extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginPage() {
        super();
    }

    @Override
    public void init() throws ServletException {
        WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
        AutowireCapableBeanFactory bf = ctx.getAutowireCapableBeanFactory();

//      bf.autowireBean(this);
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void service(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();

        out.println("<html>");
        out.println("<head>");
        out.println("<title> TESTE </title>");
        out.println("</head>");
        out.println("<body>");
        out.println("Hello World!");
        out.println("</body>");
        out.println("</html>");
    }


}

问题: 当我点击提交按钮时,它会显示错误:

**type Status report

message /Apontador_Web/LoginPage

description The requested resource is not available.**

当我删除标签“listener”和“context-param”时 它加载servlet。

可能会发生什么?

2 个答案:

答案 0 :(得分:1)

转到 glassfish服务器输出并查找一些异常

告诉我例外

的名称

全部打印

答案 1 :(得分:0)

您需要在类名@WebServlet(urlPatterns={"/LoginPage"})

之上添加public class LoginPage extends HttpServlet {