Hey guys!!! Just found this wonderful forum.....Thanks for the guys who run this forum..

I am an Elec. & Telecom engineer...
I understand Java Programming & I have more than 90% of the concepts of core-java and good knowledge of jsp,servlets,struts....I have some basic knowledge of oracle....

I have a excellent logical & quant aptitude...
What I really struggle in is putting my logic in terms of java....

For eg: A program to show whether the entered string is palindrome or not.
            I know that for a string to be palindrome 1st half of the string elements must be same as the other half....I am able to form the for loop....but struggle in what to write inside the for-loop (coding)..this is just an example....


    for(int i=0;i<=n/2;i++)
    {I struggle here}




What should i do?? I know all the basic concepts related to core-java. I have learnt everything from arrays,threads, generics,collections,string,string-builder....

I know i am not from CompSci or IT background....but i am placed in an it company as a software developer  and undergoing training for the same....

I am required to complete a project Shopping cart portal to pass....(In a week!!!)otherwise i think i will be fired...

I managed to build static home page using HTML & CSS..(not the toughest part i know..)...But for the final I have to create a dynamic page. I have prepared a database for everything(Logo,header,footer,categories)... but i am not able to display it....
for eg: String Header stores my header info... it gets it from database but displays version of the browser which i use....

i use
        ${header}



                        this is my code:
                        Database connection page:
                        /*
                         * To change this template, choose Tools | Templates
                         * and open the template in the editor.
                         */
                        package model;

                        import java.sql.*;
                        import java.sql.Connection;
                        import javax.servlet.http.HttpServletRequest;

                        import org.apache.struts.action.ActionErrors;
                        import org.apache.struts.action.ActionMapping;
                        import org.apache.struts.action.ActionMessage;

                        /**
                         *
                         * @author Ankur5
                         */
                        public class Bean extends org.apache.struts.action.ActionForm {

                            String url = "jdbc:oracle:thin:@localhost:1521:XE";
                            String username = "xxx";
                            String password = "xxx";
                            String header;
                            Connection con;

                            public DbCon() throws ClassNotFoundException, SQLException {
                                getConnection();
                            }

                            public void getConnection() throws ClassNotFoundException, SQLException {
                                Class.forName("oracle.jdbc.OracleDriver");
                                con = DriverManager.getConnection(url, username, password);
                            }

                            public String getHeader() throws SQLException {
                                Statement createStatement = con.createStatement();
                                ResultSet rs = createStatement.executeQuery("select * from CONFIG;");
                                while (rs.next()) {
                                    header = rs.getString(2);
                                }

                                return header;

                            }
                            /**
                             * This is the action called from the Struts framework.
                             *
                             * @param mapping The ActionMapping used to select this instance.
                             * @param request The HTTP Request we are processing.
                             * @return
                             */
                            /*public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
                             * ActionErrors errors = new ActionErrors();
                             * if (getUsername() == null || getUsername().length() < 1) {
                             * errors.add("name", new ActionMessage("error.name.required"));
                             * // TODO: add 'error.name.required' key to your resources
                             * }
                             * return errors;
                             * }*/
                        }

                        ActionClass

                        /*
                         * To change this template, choose Tools | Templates
                         * and open the template in the editor.
                         */
                        package com.myapp.struts;

                        import model.Bean;
                        import javax.servlet.http.HttpServletRequest;
                        import javax.servlet.http.HttpServletResponse;
                        import javax.servlet.http.HttpSession;
                        import org.apache.struts.action.ActionForm;
                        import org.apache.struts.action.ActionForward;
                        import org.apache.struts.action.ActionMapping;

                        /**
                         *
                         * @author Ankur5
                         */
                        public class ActionObject extends org.apache.struts.action.Action {

                            /* forward name="success" path="" */
                            private static final String SUCCESS = "success";
                            String header;

                            /**
                             * This is the action called from the Struts framework.
                             *
                             * @param mapping The ActionMapping used to select this instance.
                             * @param form The optional ActionForm bean for this request.
                             * @param request The HTTP Request we are processing.
                             * @param response The HTTP Response we are processing.
                             * @throws java.lang.Exception
                             * @return
                             */
                            @Override
                            public ActionForward execute(ActionMapping mapping, ActionForm form,
                                    HttpServletRequest request, HttpServletResponse response)
                                    throws Exception {

                                DbCon db = new DbCon();
                                header = db.getHeader();
                                HttpSession s = request.getSession();
                                s.setAttribute("header", header);





                                return mapping.findForward(SUCCESS);
                            }
                        }



I used HttpSession to bind header to session and display..
Is there any error. I dont want you guys to give programs but learn programming. suggest me an approach to MASTER JAVA because I feel that knowing what to be done but not able to put it in programming is just Dumb....

Dani AI

Generated

A few practical steps for turning a plain logic idea into Java code (and a quick fix for the Struts/EL symptom in this thread).

Start with very small, testable steps: write the algorithm in plain English (or numbered pseudocode), pick simple examples and trace them by hand, then translate each step into a tiny method. For the palindrome example that clarified (reads the same forward and backward), the core check is pairwise character comparison from the ends toward the center. A compact, testable implementation looks like this:

public static boolean isPalindrome(String s) {
    if (s == null) return false;
    s = s.replaceAll("\\s+", "").toLowerCase();
    int n = s.length();
    for (int i = 0; i < n / 2; i++) {
        if (s.charAt(i) != s.charAt(n - 1 - i)) return false;
    }
    return true;
}

Regarding the dynamic header showing the browser/version: JSP Expression Language has implicit objects (including header and headerValues) that map HTTP request headers, and there are scoped maps such as sessionScope and requestScope. If an attribute name collides with an EL implicit name it can produce unexpected results; use an explicit scope (for example ${sessionScope.header}) or use a different attribute name such as siteHeader to avoid collisions. Also, FormBeans/ActionForms should not hold DB connections or business logic — fetch data from a DAO/Service in the Action and place only simple data (String) into request/session for the view. (docs.oracle.com)

A few production-level cautions: never keep a live JDBC Connection in a form or session. Always release JDBC resources (ResultSet/Statement/Connection) — prefer try-with-resources or container DataSource/connection-pooling, and use PreparedStatement and explicit column names for clarity and safety. These patterns avoid leaks and scalability problems and make debugging much easier. (docs.oracle.com)

Small iterations — pseudocode, a tiny tested method, then refactor into DAO/Action/JSP — build confidence quickly.

Recommended Answers

All 2 Replies

First, the first half of a palindrom is not the same as the second half. A palidrom is a word that reads the same forward or backward. This is something quite different! Here is a helpful Wikipedia article about them: http://en.wikipedia.org/wiki/Palindrome

I don't have time to review your code right now (need to go get dinner for wife), but you will likely need to adjust it in light of these differences.

Sorry.. I was not clear before... What I meant to say was 1st element(Letter) should be same as last....& so on.. Until middle element...So n/2 times for loop will execute...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.