如何在servlet中处理http URL请求

问题描述 投票:0回答:1

我正在学习J2EE。我尝试将图像插入数据库的简单程序,当我执行该程序时,我得到以下错误消息:“类型状态:报告消息:此URL不支持HTTP方法POST描述:在请求行中收到的方法是已知的由原始服务器提供,但不受目标资源支持]

servlet:

    import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class dataentry extends HttpServlet{
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
PrintWriter pw = response.getWriter();
String connectionURL = "jdbc:mysql://localhost:3306/phackathon?zeroDateTimeBehavior=convertToNull";
Connection con=null;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(connectionURL, "root", "");
PreparedStatement ps = con.prepareStatement("INSERT INTO pictures VALUES(?,?)");
File file = new File("C:/mom2.jpg");
FileInputStream fs = new FileInputStream(file);
ps.setInt(1,1);
ps.setBinaryStream(2,fs,fs.available());
int i = ps.executeUpdate();
if(i!=0){
pw.println("image inserted successfully");
}else{
pw.println("problem in image insertion");
}
} catch(Exception e){
System.out.println(e);
}
}
}

jsp:上传图片

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>

        <form action="UploadImage.do"  enctype="multipart/form-data" name="productForm" id="productForm" method="POST"><br><br>
        <table width="400px" align="center" border=0 style="background-color:ffeeff;">
            <tr>
                <td align="center" colspan=2 style="font-weight:bold;font-size:20pt;">Image Details</td>
            </tr>
            <tr>
                <td align="center" colspan=2>&nbsp;</td>
            </tr>
            <tr>
                <td>Image Link: </td>
                <td>
                    <input type="file" name="file" id="file">
                <td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" name="Submit" value="Submit"></td>
            </tr>
            <tr>
                <td colspan="2">&nbsp;</td>
            </tr>
        </table>
    </form>
    </body>
</html>

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">
    <servlet>
        <servlet-name>dataentry</servlet-name>
        <servlet-class>dataentry</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>dataentry</servlet-name>
        <url-pattern>/UploadImage.do</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>
java servlets
1个回答
0
投票

看来在服务器端您已经实现了GET方法,而从jsp您正在尝试调用POST方法。在您的情况下,您尝试保存一些内容,因此POST应该是您的选择。

© www.soinside.com 2019 - 2024. All rights reserved.