1 . How to create website in java without jsp?

Answer :

 JSP is just an application framework *intended* to simplify the use of the J(2)EE servlet specification for the view aspects of apps built using the MVC architectural pattern. JSPs are compiled (by the servlet container) to generate...you guessed it - servlets! 

You absolutely CAN use the original, base, J(2)EE servlet model to develop websites without employing JSPs at all. It will require that you write your own code to implement the interface HttpServlet:

Probably, you'll want to start your own subclassing from the level of the extending, implementing J(2)EE classes HttpServletRequestWrapper and -ResponseWrapper:

You're going to need to write your own code to handle things related to the view layer, to replace things that are already included in the JSP model. I'd suggest you look into some sort of view template framework (google "java view template" for suggestions). 

Been there -done that. If you need more hints, ask (I ain't a big fan of JSP - I like customized templating frameworks).


Leave a Comment

Name  
  Email   
Message
1 . Apache tomcat <error-page> tag in conf/web.xml not rendering an error page?
 

Answer :

using error-code instead of exception-type

<error-page>
    <error-code>403</error-code>
    <location>/error.jsp</location>
</error-page>

2 . Passing an ArrayList from Struts1.3 action to appropriate JSP?
 

Answer :

Remove attribute redirect="true" from the forward config it makes lose of attributes required for jsp redirected.

How can you be assure about action class if it doesn' t make sense to jsp? The values are evaluated by the EL expressions and rendered by Jstl tags, so you have to make sure they are accessible in the scope of jsp. Forwarding to jsp saves the variables to request scope then you can read them in the for each tag.


3 . Could not get values from request.getParameter Java JSP?
 

Answer :

disabled elements in a form will not be submitted. You may have to use readonly.


4 . I am trying to print result on same page using ajax query in java servlet form [duplicate]?
 

Answer :

You never actually call the post function with your ajax code or stop the form from loading the register page.

If you called the post function then checked your console you might see something like Uncaught TypeError: document.getElementByName is not a function, because the function is getElementsByName(notice the s) and it returns a list of elements which needs to be taken into account.
Since you're using jQuery you could just use .serialize() on the form to get its field data.

$(document).ready(function(){

    $('form').submit(function()
    {
        $.ajax
        ({
            type: 'post',
            url: 'RegisterServlet.java',
            data: $(this).serialize(),
            success: function (response)
            {
                document.getElementsByName("insert")[0].innerHTML="Form Successfully Registerd";
            }
        }); 
        return false;
    });
});

5 . Thymeleaf list within list input?
 

Answer :

Template: Create unique "modal" for each job. (work-list.html)

<form method="post" action="#" th:action="@{/work-list}" th:object="${workHistoryDetails}">
 <table>
  <thead>
   <tr>
    <th>
      <button type="submit" name="addRow" th:text="'Add Row'">Add row</button>
    </th>
   </tr>
  </thead>
  <tbody>
   <tr id='addr_work0' th:each="workRow, rowStat : *{jobs}">
    <td th:text="${rowStat.index + 1}"></td>
      <td><input type="text" 
                placeholder='Company Name'  class="form-control" th:field="*{jobs[__${rowStat.index}__].companyName}"/></td>
      <td><input type="text" 
                placeholder='Company city' class="form-control" th:field="*{jobs[__${rowStat.index}__].city}"/></td>


      <td><button type="button" class="btn btn-primary" data-toggle="modal" th:attr="data-target='#myModal'+${rowStat.index}" >Open modal</button></td>

      <!-- The Modal -->
      <div class="modal fade" th:id="'myModal'+${rowStat.index}">
       <div class="modal-dialog">
        <div class="modal-content">

         <!-- Modal Header -->
         <div class="modal-header">
           <h4 class="modal-title">Modal Heading</h4>
           <button type="button" class="close" data-dismiss="modal">&times;</button>
         </div>

         <!-- Modal body -->
         <div class="modal-body">
           <input type="text"
                placeholder='Company Name'  class="form-control" th:field="*{jobs[__${rowStat.index}__].workDoneSentences[0]}"/>
           <input type="text" 
                placeholder='Company city' class="form-control" th:field="*{jobs[__${rowStat.index}__].workDoneSentences[1]}"/>
         </div>

         <!-- Modal footer -->
         <div class="modal-footer">
           <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
         </div>

        </div>
       </div>
      </div>
    </tr>
   </tbody>
  </table>
 <input type="submit" name="submit" value="Add" class="btn btn-danger" />
</form> 

Controller:

@ModelAttribute("workHistoryDetails")
public WorkHistory populateWorkHistories() {
    return this.workHistory.getAllHistoryDetail();
}

@Autowired
WorkHistoryService workHistory;

@GetMapping("/work-list")
public String greeting(final WorkHistory workHistoryDetails) {
    return "work-list";
}

@RequestMapping(value="/work-list",method=RequestMethod.POST)
public String create(final WorkHistory workHistoryDetails) {

   this.workHistory.setJobs(workHistoryDetails);
   return "work-list";
}

// for adding new row job to jobs list
@RequestMapping(value="/work-list", params={"addRow"})
public String addRow(final WorkHistory workHistoryDetails, Model model) {

    Jobs jobRow = new Jobs("","",new ArrayList<>());
    workHistoryDetails.getJobs().add(jobRow);
    model.addAttribute("workHistoryDetails", workHistoryDetails);
    return "work-list";
}

Service: Just for the sake of testing:

@Service
public class WorkHistoryService {

    static WorkHistory workHistoryDetails =new WorkHistory();
    static{
        List<String> workDones = new ArrayList<>();
        workDones.add("angular");
        workDones.add("description");
        List<String> workDones1 = new ArrayList<>();
        workDones1.add("java,c++");
        workDones1.add("description also");

        Jobs job1 = new Jobs("Twitter", "USA",workDones); 
        Jobs job2 = new Jobs("Microsoft", "USA",workDones1);
        List<Jobs> jobList = new ArrayList<>();
        jobList.add(job1);
        jobList.add(job2);
        workHistoryDetails.setJobs(jobList);
    }

    public WorkHistory getAllHistoryDetail(){

        return workHistoryDetails;
    }

    public void setJobs(WorkHistory workHistory){
        workHistoryDetails.getJobs().clear();
        List<Jobs> jobs = workHistory.getJobs();
        for (Jobs job : jobs) {
            workHistoryDetails.getJobs().add(job);
        }
    }
}

Jobs domain:

public class Jobs {
   private String companyName;
   private String city;
   private List<String> workDoneSentences;
    //default constructor important!   
    public Jobs() {
    super();
    }
   //setter getter
}

I hope it helps you.



6 . Java EE .jsp file can't be accessed through localhost?
 

Answer :

I had the same issue, it was because of Wildfly 11. I downgraded it to Wildfly 10.1 and it started working!


7 . Pass value Bean to Class then Class to JSP?
 

Answer :

Set the value in ServletContext in your bean class:

 @Autowired
    private ServletContext servletContext;

    private String a;
    public void setA(String a) {
       servletContext.setAttribute("a",a);
    }
Get this value in JSP using :

<%
String a=(String)getServletContext().getAttribute("a");
%>


8 . Text Area dynamically created and then gone within a second?
 

Answer :
I have tried your code and discovered that the problem is coming from your form tag if you remove the form tag it will work just fine.

<html>
    <head>
    </head>
    <body>
    <div class="container">

       <div class="row">

         <div class="col-name">
          <label for="section">Section</label>
         </div>

          <div class="col-value">
           <input type="text" id="section">
          </div>
        </div>

        <div class="row">

         <div class="col-name">
          <label for="assignmentname">Assignment Name</label>
         </div>

          <div class="col-value">
           <input type="text" id="assignmentname">
          </div>
        </div>

        <div class="row">

         <div class="col-name">
          <label for="questionnumber">Number Of Questions</label>
         </div>

          <div class="col-value">
           <input type="text" id="question_number" max="10" min="1"/>
          </div>

          <div class="col-value">
          <button onclick="generate()">Generate Assignment</button>
          </div>
        </div>

        <div id="content">

        </div>

        </div>


       <script>
     function generate(){
    var x = document.getElementById("question_number").value;
    var content = document.getElementById("content");
    var pa= new Array();
    var box = new Array();
    while(content.hasChildNodes()){
        content.removeChild(content.lastChild);
    }

    var i=0;
    while(i<x){
        pa[i]=document.createElement("p");
        var box =document.createElement("textarea");
        box.style.minHeight="100px";
        box.style.minWidth="300px";
        box.style.animation="moveToPos 5s 1";
        box.style.animationFillMode="forwards";
        pa[i].appendChild(box);
        content.appendChild(pa[i]);
        i++;
      }
}
       </script>

     </body>
    </html>

9 . List not visible on when sent from Spring controller to JSP?
 

Answer :

<c:if test="${not empty lists}">

To:

<c:if test="${not empty mediaList}">

10 . Problem with while( rs.next()) in jsp?

Answer :

  Well, yeah. It's hard to tell without the indenting, but it looks like all your code paths redirect to another page during the first rs loop. I don't know the particulars of JSP, but it probably stops execution once it sees that redirect.



11 . How to embed jsp coding into javascript method?how to assing javascript variable in jsp session in javascriptm?

Answer :

You might NOT remember, but English language has some grammar rules AND punctuation... Using that helps readers to understand what YOU are trying to say... 

JSP - server side scripting technology, it runs on web server and generates HTML/JavaScript that gets sent to client (browser), ALL generated content gets sent to client (browser) AFTER it FINISHED it's job... 
JavaScript - client side scripting language, it runs on client (browser) AFTER the page is received by the client (browser)... 

The only way to send values from JSP to JavaScript is to generate some JavaScript statements in JSP (assign a JavaScript value...) 
The only way to send values from JavaScript to JSP is to request something from web server (AJAX or simple GET or POST requests)...


12 . How do i implement pop ups using JSP?

Answer :

You r to do it in javascript. If u want to use it in ur JSP page U can use STRUTS tags to include ur javascript code. 
STRUTS has the following tag attributes 
onmousedown 
onmousemove 
onmouseout 
onmouseover 
onmouseup 


13 . What is Best Web Language for Website designing & as well as CGI Scripting Perl/Java/Php/ HTML/XML/JSP etc ???

Answer :

 You obviously have to use HTML, but as a server side language, I suggest PHP, it is lightweight and you can do nice database stuff with it. 

I am a bit biased as I have not used Perl or JSP, but have used the others, and of which I find PHP works the best. 

But first decide on your needs, and then do research on the benefits of each and choose according to your requirements.


14 . Difference between java script an jsp?

Answer :

 Java is a programming language and it requires compilation before it can be executed where is JSP(Java Server Pages) is server side scripting technology which don't need compilation. t is being interpreted at the runtime!


15 . How to connect jsp with oracle?

Answer :

  It is very easy to connect to oracle database from a JSP though JSP are used to cater the view part of the application. 
You can use classes12.jar file to connect to oracle database. 
put the following code in the scriplet and ensure that you close the connection in the finally block :) 

-Surya Rao.

Source(s)://place the code in the JSP 
<% 
//register driver 
DriverManager.registerDriver("oracle.j... 
//get the database connection 
Connection con = 
DriverManager.getConnection ("jdbc:oracle:thin:@<<DBServerIPaddress>... 
"<<userName>>", "<<password>>"); 
// Create statement 
Statement stmt = con.createStatement (); 
ResultSet rs = stmt.executeQuery ("select * from Emp"); 

// Iterar mientras haya datos. 
while (rset.next ()) 
System.out.println (rset.getString ("empNumber")); 

%>