本文介绍了空白页面即将出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Quote:

我正在尝试在sql数据库中插入员工详细信息但是当我运行程序时它会在输入后显示注册页面详细信息输出未显示为空白页

i'm trying to insert employee details in sql database but when i run the program it is showing the registration page after entering details output is not showing as blank page





我尝试过:



web.xml



What I have tried:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <servlet>
        <servlet-name>CreateServlet</servlet-name>
        <servlet-class>com.sai.CreateServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>CreateServlet</servlet-name>
        <url-pattern>/CreateServlet</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>







public class CreateServlet extends HttpServlet
{


    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        Connection con=null;
        Statement st=null;
       try
       {
        Class.forName("com.mysql.jdbc.Driver");
        DriverManager.getConnection("jdbc:mysql://localhost:3306/dbemployee","root","");
        con.createStatement();
        String id=request.getParameter("UserName");
        String pwd=request.getParameter("Password");
        String eAdd=request.getParameter("EmpAddress");
        String gender=request.getParameter("MaleorFemale");
        String email=request.getParameter("Email");
        String lang=request.getParameter("languages");
        String nation=request.getParameter("Nationality");
        String date=request.getParameter("RegDate");

        String sql="insert into Employee Values ('"+id+"' '"+pwd+"' '"+eAdd+"' '"+gender+"'  '"+email+"'  '"+lang+"'  '"+nation+"' '"+date+"')";
        st.executeUpdate(sql);
        if(id !=null | pwd != null | eAdd!=null | gender !=null |email !=null | lang !=null | nation!=null | date !=null)
        {
            RequestDispatcher rd = request.getRequestDispatcher("/success.html");
            rd.forward(request, response);
        }
        else
        {
            out.println("<font color=red>Please fill all the fields</font>");
            RequestDispatcher rd = request.getRequestDispatcher("/index.html");
            rd.forward(request, response);

        }

       }catch(ClassNotFoundException | SQLException  | NullPointerException e)
       {
           e.printStackTrace();
       }
       finally
		{
			try
			{
				if(st!=null) st.close();
				if(con!=null)con.close();
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
    }

    // <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
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(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);
    }

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

}

推荐答案

String sql="insert into Employee Values ('"+id+"' '"+pwd+"' '"+eAdd+"' '"+gender+"'  '"+email+"'  '"+lang+"'  '"+nation+"' '"+date+"')";
st.executeUpdate(sql); // this line
if(id !=null | pwd != null | eAdd!=null | gender !=null |email !=null | lang !=null | nation!=null | date !=null)
{
    // should go here
    RequestDispatcher rd = request.getRequestDispatcher("/success.html");
    rd.forward(request, response);
}
else
{
    out.println("<font color=red>Please fill all the fields</font>");
    RequestDispatcher rd = request.getRequestDispatcher("/index.html");
    rd.forward(request, response);

}







String sql="insert into Employee Values ('"+id+"' '"+pwd+"' '"+eAdd+"' '"+gender+"'  '"+email+"'  '"+lang+"'  '"+nation+"' '"+date+"')";



不是你问题的解决方案,而是你遇到的另一个问题。 />
永远不要通过连接字符串来构建SQL查询。迟早,您将使用用户输入来执行此操作,这会打开一个名为SQL注入的漏洞,这对您的数据库很容易并且容易出错。

名称中的单引号你的程序崩溃。如果用户输入像Brian O'Conner这样的名称可能会使您的应用程序崩溃,那么这是一个SQL注入漏洞,崩溃是最少的问题,恶意用户输入,并且它被提升为具有所有凭据的SQL命令。

[]

[]

[]

[]

[ ]

[]


Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]


这篇关于空白页面即将出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 04:19
查看更多