Home>

Strats 1 is trying to build a simple system.

After confirming that the page transition of jsp has been made and that information can be searched by accessing the database, the view and model were rewritten and connected with the action file. .

How do applications work?

ipEntry.jsp

<% @ page language = "java" contentType = "text/html;charset = UTF-8"
    pageEncoding = "UTF-8"%>
<% @ taglib uri = "/ WEB-INF/struts-html.tld" prefix = "html"%>
<% @ taglib uri = "/ WEB-INF/struts-bean.tld" prefix = "bean"%>
<% @ taglib uri = "/ WEB-INF/struts-logic.tld" prefix = "logic"%>

<html: html>
<body>
     Guhe 
  <html: form action = "/ Ipe">
    <html: text name = "IpForm" property = "pcID" size = "10" />
    <html: submit value = "=" />
  </html: form>
</body>
</html: html>


ipResult.jsp

<% @ page language = "java" contentType = "text/html;charset = UTF-8"
    pageEncoding = "UTF-8"%>
<! DOCTYPE html>
<html>
<head>
<meta charset = "UTF-8">
<title>Insert title here</title>
</head>
<body>
  <bean: write name = "IpForm" property = "ip" />
</body>
</html>


ipForm.java

package form;
import org.apache.struts.action.ActionForm;
public class IpForm extends ActionForm {
  private String pcID = "";
  private String pcName = "";
  private String ipAddress = "";
  public IpForm (String pcID, String pcName, String ipAddress) {
    // TODO auto-generated constructor stub
      this.pcID = pcID;
      this.pcName = pcName;
      this.ipAddress = ipAddress;
  }
  public String getPcID () {
      return pcID;
  }
  public void setPcID (String pcID) {
      this.pcID = pcID;
  }
  public String getPcName () {
      return pcName;
  }
  public void setPcName (String pcName) {
      this.pcName = pcName;
  }
  public String getIpAddress () {
      return ipAddress;
  }
  public void setIpAddress (String ipAddress) {
      this.ipAddress = ipAddress;
  }
}

struts-config.xml

<? xml version = "1.0" encoding = "ISO-8859-1"?>
<! DOCTYPE struts-config PUBLIC
          "-// Apache Software Foundation // DTD Struts Configuration 1.2 // EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<!-============================================== == Form Bean Definitions->
    <form-beans>
        <form-bean type = "form.CalcForm" name = "CalcForm" />
        <form-bean type = "form.CalcForm2" name = "AAA" />
        <form-bean type = "view.CalcView" name = "CalcView" />
        <form-bean type = "form.IpForm" name = "IpForm" />
   </form-beans>

<!-=========================================== Global Forward Definitions ->
    <global-forwards>
        <!-Default forward to "Welcome" action->
        <!-Demonstrates using index.jsp to forward->
        <forward
            name = "welcome"
            path = "/ pages/Calc2.jsp" />
    </global-forwards>

<!-=========================================== Action Mapping Definitions ->
    
    <!-Calculation->
        
        <forward
            name = "success"
            path = "Calc" />
        </action>
    <!-Self-made calculation->
        
        <forward
            name = "success"
            path = "/ pages/Calc2.jsp" />
        </action>
        
        <forward
            name = "success"
            path = "/ pages/ipResult.jsp" />
        </action>

    </action-mappings>

<!-============================================= Controller Configuration->
    <controller
       processorClass = "org.apache.struts.tiles.TilesRequestProcessor" />

<!-======================================== Message Resources Definitions->
    <message-resources parameter = "MessageResources" /></struts-config>

ipAction

package action;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import form.IpForm;
public class IpAction {
    public
    ActionForward execute (
    ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response)
    throws
        SQLException,
        NamingException,
        IllegalAccessException,
        java.lang.reflect.InvocationTargetException,
        Exception {
        request.setCharacterEncoding ("UTF-8");
        String pcID = request.getParameter ("pcID");
        String pcName = request.getParameter ("pcName");
        String ipAddress = request.getParameter ("ipAddress");
        final String JDBC_URL = "jdbc: mysql: // localhost: 3306/pcm? serverTimezone = JST";
        final String DB_USER = "root";
        final String DB_PASS = "y21password";
            try (Connection conn = DriverManager.getConnection (JDBC_URL, DB_USER, DB_PASS)) {
                String sql
                    = "SELECT mng_pc.pc_id, pc_name, ip_address" +
                            "FROM pcm.mng_pc" +
                            "JOIN pcm.ip_address" +
                            "on mng_pc.pc_id = ip_address.pc_id where mng_pc.pc_id =?";
                PreparedStatement pStmt = conn.prepareStatement (sql);
                pStmt.setString (1, pcID);
                ResultSet rs = pStmt.executeQuery ();

                while (rs.next ()) {
                    IpForm iform = new IpForm (pcID, pcName, ipAddress);
                        BeanUtils.setProperty (iform, "id", rs.getString ("pc_id"));
                        BeanUtils.setProperty (iform, "name", rs.getString ("pc_name"));
                        BeanUtils.setProperty (iform, "ip", rs.getString ("ip_address"));
                    request.setAttribute ("IpForm", iform);
                }
            } catch (SQLException e) {
                e.printStackTrace ();
                return null;
            }
            return (mapping.findForward ("success"));
        }
}

Error message

type Exception report
An exception occurred at line [14] while processing message [pages/ipEntry.jsp].
Explanation The server has encountered an unexpected condition. It prevents the request from executing.
exception
org.apache.jasper.JasperException: An exception occurred at line [14] while processing [pages/ipEntry.jsp].11:
12:<body>
13:  Go to 
14:<html: form action = "/ Ipe">
15:<html: text name = "IpForm" property = "pcID" size = "10" />
16:<html: submit value = "=" />
17:</html: form>

Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException (JspServletWrapper.java:625)
    org.apache.jasper.servlet.JspServletWrapper.service (JspServletWrapper.java:499)
    org.apache.jasper.servlet.JspServlet.serviceJspFile (JspServlet.java:385)
    org.apache.jasper.servlet.JspServlet.service (JspServlet.java:329)
    javax.servlet.http.HttpServlet.service (HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter (WsFilter.java:53)
    filter.EncodingFileter.doFilter (EncodingFileter.java:30)
Root cause
javax.servlet.ServletException: javax.servlet.jsp.JspException: Exception when generating a bean of class form.IpForm: {1}
    org.apache.jasper.runtime.PageContextImpl.handlePageException (PageContextImpl.java:666)
    org.apache.jsp.pages.ipEntry_jsp._jspService (ipEntry_jsp.java:159)
    org.apache.jasper.runtime.HttpJspBase.service (HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service (HttpServlet.java:741)
    org.apache.jasper.servlet.JspServletWrapper.service (JspServletWrapper.java:476)
    org.apache.jasper.servlet.JspServlet.serviceJspFile (JspServlet.java:385)
    org.apache.jasper.servlet.JspServlet.service (JspServlet.java:329)
    javax.servlet.http.HttpServlet.service (HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter (WsFilter.java:53)
    filter.EncodingFileter.doFilter (EncodingFileter.java:30)
Root cause
javax.servlet.jsp.JspException: Exception when generating a bean of class form.IpForm: {1}
    org.apache.struts.taglib.html.FormTag.initFormBean (FormTag.java:487)
    org.apache.struts.taglib.html.FormTag.doStartTag (FormTag.java:457)
    org.apache.jsp.pages.ipEntry_jsp._jspx_meth_html_005fform_005f0 (ipEntry_jsp.java:219)
    org.apache.jsp.pages.ipEntry_jsp._jspx_meth_html_005fhtml_005f0 (ipEntry_jsp.java:185)
    org.apache.jsp.pages.ipEntry_jsp._jspService (ipEntry_jsp.java:144)
    org.apache.jasper.runtime.HttpJspBase.service (HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service (HttpServlet.java:741)
    org.apache.jasper.servlet.JspServletWrapper.service (JspServletWrapper.java:476)
    org.apache.jasper.servlet.JspServlet.serviceJspFile (JspServlet.java:385)
    org.apache.jasper.servlet.JspServlet.service (JspServlet.java:329)
    javax.servlet.http.HttpServlet.service (HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter (WsFilter.java:53)
    filter.EncodingFileter.doFilter (EncodingFileter.java:30)
Note All stack traces of the cause are logged in the server log

Because the bean has not been created, I thought that it would not be possible to go down while the ipAction while, but I don't know how to fix it from here.

I hope you can borrow your wisdom.

The following environment

Windows
Microsoft Windows [Version 10.0.17763.737]
Widnows10 Pro version 1809
Eclipse
Version: 2019-09 R (4.13.0)
Build id: 20190917-1200

Tomcat
9.0
Java
C: \ Program Files (x86) \ Java \ jre1.8.0_221
C: \ Program Files (x86) \ Java \ jdk1.5.0
Mysql
mysql Ver 8.0.17 for Win64 on x86_64 (MySQL Community Server-GPL)
mysql-connector-java-8.0.17.jar

Struts
Specification-Version: 1.2.9 (first struts project)