-1

我在 struts2 MVC 中创建了一个项目。谁能告诉我如何在不直接通过 struts2.xml 的情况下访问 servlet?我知道我们可以通过动作调度程序通过 struts2.xml 访问,但我想知道是否有任何其他方法可以直接从 jsp 访问 servlet?

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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">
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <servlet>
        <servlet-name>MyServlet</servlet-name>
        <servlet-class>com.myservlet.MyServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>MyServlet</servlet-name>
        <url-pattern>/MyServlet</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

struts.xml

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <!-- Configuration for the default package. -->
    <package name="default" extends="struts-default">

    </package>
</struts>

MyServlet.java

package com.myservlet;

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;
import javax.swing.JOptionPane;

public class MyServlet extends HttpServlet {
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
        } finally { 
            out.close();
        }
    } 
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);

    } 

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
        PrintWriter out = response.getWriter();
        String name=request.getParameter("username");
        JOptionPane.showMessageDialog(null,"SEEE:"+name);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

索引.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!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=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form action="MyServlet" method="post">
            USERNAME:<input type="text" name="username"/><br>
            <input type="submit" value="SHOW"/>
        </form>
    </body>
</html>

在上面的代码中,为什么我不能在 getPost() 方法中访问文本字段的值?提前致谢...

4

1 回答 1

0

我相信您知道如何调用 servlet 以及如何配置它们,所以我不会详细说明。

您可以创建 servlet 并根据您的用例调用它们,您只需告诉 Struts2 过滤器您不希望 Struts2 处理某些特定的 URL

在您的struts.xml添加中:

<constant name="struts.action.excludePattern" value="/ServletToExcludeFromStruts*"/>

更多详情请参考webxml

于 2012-09-13T06:30:43.897 回答