gpt4 book ai didi

jsp - 此网址不支持 HTTP 405 发布方法

转载 作者:行者123 更新时间:2023-11-28 23:32:12 25 4
gpt4 key购买 nike

我要从一个 HTML 页面(例如 “index.jsp”)到一个 servlet(例如 servlet.java)并对其进行编程,以便从通过 输入的问题中提取关键字index.jsp 和那些作为输出显示在另一个 JSP 页面中的关键字说 output.jsp。请查看我的代码并告诉我为什么会出现错误

"http 405: post not supported by this url". 

我正在使用 tomcat 并像 localhost:8080\myprograms\index.jsp 一样运行它此外,我不知道如何在 url 模式 \index.jsp\servlet 中编写 web.xml。是否需要在web.xml

中进入 output.jsp页面

index.jsp


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html>
</head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>
Query search
</title>
<style>
BODY { background: url(mag.jpg); background-repeat: no-repeat }
</style>
</head>
<body>
<a href="1.html">Exit</a>
<img src="home.png" align="left" height="180" width="130">
<center>
<form action="servlet1" method="post"></br></br></br></br></br></br></br></br>
<font face="Broadway" color="white" align="left">Search........!!!!</font></br></br>
<input type="text" name="query" size="100"></br></br></br></br>
<input type="SUBMIT" value="Submit Query">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="RESET" value="Reset">
</form>
</center></br></br></br></br></br></br></br></br></br></br></br>
<font color="white"><marquee behavior="alternate">Note:Only generalized query that starts as "who", "what","give"..</marquee></font>
</body>
</html>

输出.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Output Page</title>
</head>
<body align="center">
<form method="GET" action="servlet1">
<p>${z}</p>
</body>
</html>

servlet1.java

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

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 java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;
/**
*
* @author hp
*/
@WebServlet(urlPatterns = {"/servlet1"})
public class servlet1 extends HttpServlet {

/**
* Processes requests for both HTTP
* <code>GET</code> and
* <code>POST</code> methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/


// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP
* <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();
String txt=request.getParameter("query");
if (txt.matches("Who is the(.*)")) {
String re1=".*?";
String re2="(?:[a-z][a-z]+)";
String re3=".*?";
String re4="(?:[a-z][a-z]+)";
String re5=".*?";
String re6="(?:[a-z][a-z]+)";
String re7=".*?";
String re8="((?:[a-z][a-z]+))";
String re9=".*?";
String re10="(?:[a-z][a-z]+)";
String re11=".*?";
String re12="((?:[a-z][a-z]+))";

Pattern p = Pattern.compile(re1+re2+re3+re4+re5+re6+re7+re8+re9+re10+re11+re12,Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
Matcher m = p.matcher(txt);
if (m.find())
{
String word1=m.group(1);
String word2=m.group(2);
String z=word2.toString()+"|"+word1.toString()+"\n";
request.setAttribute("z",z);
request.getRequestDispatcher("/output.jsp").forward(request, response);
}
}

/**
* Handles the HTTP
* <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
/*@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//processRequest(request, response);
doGet(request, response);
}*/

/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}

最佳答案

您在 servlet 中收到此错误,doPost 方法已被注释,它将处理您的Post 类型请求

  /*@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//processRequest(request, response);
doGet(request, response);
}*/

关于jsp - 此网址不支持 HTTP 405 发布方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29009419/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com