我要从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 一样运行它 此外,我不知道如何在 web.xml 中编写要在url模式 \ index.jsp 或 \ servlet 中编写的内容。我是否需要在 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">
<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>
output.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>
}
答案 0 :(得分:0)
您在servlet中收到此错误,doPost
方法已被评论,该方法将处理您的Post type Request
/*@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//processRequest(request, response);
doGet(request, response);
}*/