问题描述:

To deploy the project I have purchased java server online with www.net4.in and created the database using phpmyadmin access given by them. I have deployed the project by transferring .war file of my project.

I have adjusted all the links like:

  • servlet path
  • form submission path
  • web.xml
  • all the required ones.

In servlet path if I add .java to servlet it is showing the code online, however the same path is giving error when I am submitting the form to Servlet.

<html>

<body>

<form id="theForm" name="form1" method="post" action="../../../db/src/com/mayuri/servlet/SelectCustServlet" enctype="multipart/form-data" target="_self" onSubmit="return verify()">

<fieldset class="login"><br>

<legend>Customer Details - Fill Customer Details</legend>

<div>

<label for="Ac_No">Account Number</label>

<input type="text" id="Ac_No" name="Ac_No" autocomplete="off" onKeyPress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;" />

</div>

<div>

<label for="Center_No">Center Number</label> <input type="text" id="Center_No" autocomplete="off" class="input username" name="Center_No" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;" />

</div>

</fieldset><br>

<center>

<button type="submit" >Submit</button>

</center>

</form>

</body>

</html>

Servlet Code

package com.mayuri.servlet;

import java.io.IOException;

import java.io.InputStream;

import java.io.PrintWriter;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import javax.servlet.ServletException;

import javax.servlet.annotation.MultipartConfig;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.Part;

/**

* Servlet implementation class FileUploadDBServlet

*/

//@WebServlet("/FileUploadDBServlet")

@MultipartConfig(maxFileSize = 10177215) // upload file's size up to 16MB

public class SelectCustServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

/**

* @see HttpServlet#HttpServlet()

*/

public SelectCustServlet() {

super();

}

private String dbURL = "jdbc:mysql://118.67.244.52/databasename";

private String dbUser = "loginid";

private String dbPass = "pwd";

protected void doPost(HttpServletRequest request,

HttpServletResponse response) throws ServletException, IOException {

Connection conn = null; // connection to the database

PrintWriter out = response.getWriter();

String message = null; // message will be sent back to client

try {

DriverManager.registerDriver(new com.mysql.jdbc.Driver());

conn = DriverManager.getConnection(dbURL, dbUser, dbPass);

// gets values of text fields

String Ac_No = request.getParameter("Ac_No");

System.out.println("in Servlettttttttttttttttttttttttt" + Ac_No);

String Center_No = request.getParameter("Center_No");

try {

// connects to the database

// constructs SQL statement

String sql = "INSERT INTO cust (Ac_No, Center_No ) values (?,?)";

PreparedStatement statement = conn.prepareStatement(sql);

statement.setString(1, Ac_No);

statement.setString(2, Center_No);

out.println("dfsfsdf");

// sends the statement to the database server

int row = statement.executeUpdate();

if (row > 0) {

// message = "File uploaded and saved into database";

}

} catch (SQLException ex) {

message = "ERROR: " + ex.getMessage();

ex.printStackTrace();

}

} catch (SQLException ex) {

message = "ERROR: " + ex.getMessage();

ex.printStackTrace();

} finally {

if (conn != null) {

// closes the database connection

try {

conn.close();

} catch (SQLException ex) {

ex.printStackTrace();

}

}

// sets the message in request scope

// request.setAttribute("Message", message);

// forwards to the message page

//request.getRequestDispatcher("Message.jsp").forward(request, response);

}

}

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

id="WebApp_ID" version="2.5">

<display-name>softwarematcs</display-name>

<servlet>

<description></description>

<display-name>SelectCustServlet</display-name>

<servlet-name>SelectCustServlet</servlet-name>

<servlet-class>db.src.com.mayuri.servlet.SelectCustServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>SelectCustServlet</servlet-name>

<url-pattern>../../src/com/mayuri/servlet/SelectCustServlet</url-pattern>

</servlet-mapping>

</web-app>

网友答案:

Use short and valid Servlet URL pattern and don't forget to prefix form action with context root path. Servlet URL pattern shows in the browser URL.

Either use JSP Expression Language ${pageContext.request.contextPath} or core tag library <c:url> for getting context root relative URL path.


For example:

JSP:

<form action="${pageContext.request.contextPath}/myServlet" method="post">

web.xml:

<servlet>
    <servlet-name>SelectCustServlet</servlet-name>
    <servlet-class>db.src.com.mayuri.servlet.SelectCustServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>SelectCustServlet</servlet-name>
    <url-pattern>/myServlet</url-pattern>
</servlet-mapping>

Read more...

网友答案:

Try the following pieces of code. I've modified the web.xml to the correct servlet-class name com.mayuri.servlet.SelectCustServlet and the servlet's url-pattern to /SelectCustServlet.

Also, I've updated the HTML form's action to the correct servlet url pattern.

HTML form:

<html>
    <body>
        <form id="theForm" name="form1" method="post" action="<%=request.getContextPath()%>/SelectCustServlet"  enctype="multipart/form-data" target="_self" onSubmit="return verify()">
            <fieldset class="login"><br>
                <legend>Customer Details - Fill Customer Details</legend>
                <div>
                    <label for="Ac_No">Account Number</label>
                    <input type="text" id="Ac_No" name="Ac_No" autocomplete="off" onKeyPress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;" />
                </div>

                <div>
                    <label for="Center_No">Center Number</label> <input type="text" id="Center_No" autocomplete="off" class="input username" name="Center_No"  onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;"   />
                </div>
            </fieldset><br>
            <center>
                <button type="submit" >Submit</button>
            </center>
        </form>
    </body>
</html>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         id="WebApp_ID" version="2.5">

    <display-name>softwarematcs</display-name>
    <servlet>
        <description></description>
        <display-name>SelectCustServlet</display-name>
        <servlet-name>SelectCustServlet</servlet-name>
        <servlet-class>com.mayuri.servlet.SelectCustServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>SelectCustServlet</servlet-name>
        <url-pattern>/SelectCustServlet</url-pattern>
    </servlet-mapping>
</web-app>
相关阅读:
Top