i am unable to see the login page on the web browser - html

this is the java code:i am not getting any error indication but the output of the code is not displayed on the web server.it is showing the web address as:(http://localhost:6027/HttpSearchBar/Example). Tell me whether the local host 6027 is a valid address?
package search.com;
import java.io.IO Exception;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Example extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 102831973239L;
public void dopost (HttpServletRequest hreq, HttpServletResponse hres)throws ServletException, IOException{
System.out.println("hello");
try {
hres.setContentType("text/html");
String s1 = hreq.getParameter("username");
String s2 = hreq.getParameter("password");
ServletContext sc = getServletContext();
if ((s1.equals("abc"))&&(s2.equals("xyz"))) {
hres.sendRedirect("welcome");
}else {
PrintWriter pw = hres.getWriter();
pw.print("invalid username/password");
RequestDispatcher rd = sc.getRequestDispatcher("login.html");
rd.include(hreq, hres);
}
} catch (Exception e) {
// TODO: handle exception
System.err.print(e);
}
} }
this is html code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Example Html</title>
</head>
<body bgcolor=yellow text=blue>
<center>
<h1>
<u>LoginForm</u>
</h1>
<form action="Example" method="post">
UserName<input type="text" name="username">
Password<input type="text" name="Password">
<input type="submit" value="login" /><input type="reset">
</form>
</center>
</body>
</html>
this is web.xml code:
<?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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>HttpSearchBar</display-name>
<servlet>
<servlet-name>Example</servlet-name>
<servlet-class>Example</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Example</servlet-name>
<url-pattern>/Example</url-pattern>
</servlet-mapping>
<error-page>
<error-code>404</error-code>
<location>/Example</location>
</error-page>
<error-page>
<error-code>403</error-code>
<location>/Example</location>
</error-page>
<error-page>
<exception-type>javax.servlet.ServletException</exception-type>
<location>/Example</location>
</error-page>
<error-page>
<exception-type>java.io.IOException</exception-type>
<location>/Example</location>
</error-page>
<error-page>
<exception-type>java.lang. Throw able </exception-type>
<location>/search.com.Example</location>
</error-page>
<welcome-file-list>
<welcome-file>login.html</welcome-file>
<welcome-file>login.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
this is displayed when server is started:
Oct 22, 2018 6:41:52 PM org.apache.catalina.core.ApplicationDispatcher
invoke
WARNING: Servlet Example is currently unavailable

Send a request to http://localhost:6027/ from your browser. If you can see the default Apache Tomcat page the localhost:6027 is a valid address.
There are a couple of reasons that you might not see any response.
Java is case sensitive. Your do post method is incorrect. Replace with doPost
public void doPost(HttpServletRequest hreq, HttpServletResponse hres)
throws ServletException, IOException {
Your html password input name is name="Password", and you are getting it with lowercase p.
String s2 = hreq.getParameter("Password"); //uppercase P
Finally don't use RequestDispatcher, just redirect the page. Replace everything in your else statement.
hres.sendRedirect("login.html");

By referring to the server log that has attached, it seems that you are trying to run your servlet project on apache tomcat server. Default Tomcat port is 8080. That is you should try to access your running app on:
http://localhost:8080
if you want to customize the running port you have to do it using the server.xml file.

Related

Primefaces fileUpload Listener does not invoked on wildfly server

I have added primeFaces fileUpload in my code and it worked fine on webLogic 12.2 server But when I changed the server to wildFly 10.0.1 the fileUpload Listener does not invoked anymore I wondered what is the reason and searched for this issue without avail.
That is the filters in web.xml
<filter>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-class>org.primefaces.webapp.filter.FileUploadFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
And that is my xhtml code
<p:column headerText="#{msgs.label_uploadFile}" >
<p:fileUpload id="upload" label="#{msgs.label_uploadFile}"
fileUploadListener="#{attachmentsInquiryBean.handleFileUpload}"
mode="advanced" auto="true"/>
</p:column>
That is the Listener function
public void handleFileUpload(FileUploadEvent event) {
// do something
}
Also I added two jars in my wWEB-INF/lib folder called:
commons-fileupload-1.3.jar
commons-io-2.4.jar
Here is an example of how to upload a file using primefaces and you do not need commons-fileupload-1.3.1.jar and commons-io-2.4.jar; and also you do not need to change web.xml , for more information see this How to upload file in primefaces
java code:
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import org.primefaces.model.UploadedFile;
#ManagedBean
public class FileUploadView {
private UploadedFile file;
public UploadedFile getFile() {
return file;
}
public void setFile(UploadedFile file) {
this.file = file;
}
public void upload() {
if(file.getSize() > 0) {
FacesMessage message = new FacesMessage("Succesful", file.getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, message);
}
else{
FacesMessage message = new FacesMessage("Not Succesful", "file is not uploaded");
FacesContext.getCurrentInstance().addMessage(null, message);
}
}
}
xhtml code:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form enctype="multipart/form-data">
<p:growl id="messages" showDetail="true" />
<p:fileUpload value="#{fileUploadView.file}" mode="simple" skinSimple="true"/>
<p:commandButton value="Submit" ajax="false" actionListener="#{fileUploadView.upload}" />
</h:form>
</h:body>
</html>
I've seen the same in Wildfly 10.x with PF 6.1.
The method expression for fileUpload.getFileUploadListener() is null on Wildfly.
My workaround is to use a binding on the FileUpload component and manually setting a valid method expression.
I had the same problem on tomcat and solved it by adding allowCasualMultipartParsing="true" in META-INF/context.xml :
<Context allowCasualMultipartParsing="true">
</Context>
Hope it helps.

404 error on form submission in Java [duplicate]

This question already has answers here:
Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
(19 answers)
Closed 6 years ago.
I'm developing simple web application. I've created Dynamic Web Project in Eclipse Mars and I'm using Java 1.8 and Tomcat v8.0.36.
I've created a simple form:
<!DOCTYPE html>
<html>
<head>
<title>Coffee Advice Page</title>
</head>
<body>
<form action=”SelectCoffee.do”>
Select Coffee characteristics<p>
Color:
<select name=”color” size=”1”>
<option value=”light”> light </option>
<option value=”amber”> amber </option>
<option value=”brown”> brown </option>
<option value=”dark”> dark </option>
</select>
</br></br>
<input type="submit" value="Submit">
</form>
</body>
</html>
A Servlet:
package com.example.web;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
#SuppressWarnings("serial")
public class CoffeeSelectionServlet extends HttpServlet {
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("Inside doGet()");
PrintWriter printWriter = response.getWriter();
printWriter.println("doGet() is working fine!");
}
}
Web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>CoffeeAdvice</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<!-- <welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file> -->
</welcome-file-list>
<servlet>
<servlet-name>Servlet1</servlet-name>
<servlet-class>com.example.web.CoffeeSelectionServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servlet1</servlet-name>
<url-pattern>/SelectCoffee.do</url-pattern>
</servlet-mapping>
</web-app>
But when I start Tomcat server and submit this form, I get 404 Error with query string:
http://localhost:8080/CoffeeAdvice/%C3%A2%E2%82%AC%C2%9DSelectCoffee.do%C3%A2%E2%82%AC%C2%9D?%E2%80%9Dcolor%E2%80%9D=%E2%80%9Dlight%E2%80%9D
which is not I intended.
If I make GET request directly from browser bar:
http://localhost:8080/CoffeeAdvice/SelectCoffee.do?color=light
It works absolutely fine!
Please let me know why this query string is generated distorted like this and what I'd have to change.
Any help would be much appreciated!
The double quote sign which you are using in form action is not proper
I unescaped your url which is now
"http://localhost:8080/CoffeeAdvice/ââ¬ÂSelectCoffee.doââ¬Â?âcolorâ=âlightâ"
Fix your double quote.

Json Root Name not coming in Camel Swagger Rest Service

I am trying to create rest service using apache camel swagger component.
Now i have this camel context as:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:camel="http://camel.apache.org/schema/spring"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
<!-- a bean for user services -->
<bean id="personService" class="www.tempuri.person.PersonService"/>
<camelContext id="myCamel" xmlns="http://camel.apache.org/schema/spring">
<restConfiguration component="servlet" bindingMode="auto" contextPath="myService/rest" port="8080">
<dataFormatProperty key="prettyPrint" value="true"/>
</restConfiguration>
<!-- defines the rest services using the context-path /Equipment -->
<rest path="/getPerson/persons" consumes="application/json,application/xml" produces="application/json,application/xml">
<description>Person rest service</description>
<post uri="/search" type="www.tempuri.person.model.GetPerson"
outType="www.tempuri.person.model.GetPerson">
<description>Get Person(s)</description>
<param name="body" type="body" description="Get Person(s)" required="true"/>
<responseMessage code="400" message="Bad Request" />
<responseMessage code="200" message="Person Data" />
<responseMessage code="401" message="Unauthorized" />
<to uri="bean:personService?method=getPersons"/>
</post>
</rest>
</camelContext>
</beans>
And below is my model class GetPerson:
package www.tempuri.person.model;
import javax.xml.bind.annotation.XmlRootElement;
import com.fasterxml.jackson.annotation.JsonRootName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
#ApiModel(description = "Represents Persons")
#XmlRootElement(name="getPerson")
#JsonRootName(value = "getPerson")
public class GetPerson {
#ApiModelProperty(value = "Application Area of GetPerson", required = true)
private ApplicationArea applicationArea;
#ApiModelProperty(value = "Data Area of GetPerson", required = true)
private DataArea dataArea;
/**
* #return the dataArea
*/
public DataArea getDataArea() {
return dataArea;
}
/**
* #param dataArea the dataArea to set
*/
public void setDataArea(DataArea dataArea) {
this.dataArea = dataArea;
}
public ApplicationArea getApplicationArea() {
return applicationArea;
}
public void setApplicationArea(ApplicationArea applicationArea) {
this.applicationArea = applicationArea;
}
}
Now in case of application xml it is working good and the request structure has a root element as getPerson, but in case of JSON Request it doesn't show the root element getPerson. I know that there is something called WRAP_ROOT_VALUE which can be used to enable the root name in case of json. But the problem is i don't know how to do it using camel-config.xml.
How can i set the WRAP_ROOT_VALUE through my camel-config.xml or is there some other way. Thanks in advance..

HTML(5) Friendly Markup in JSF 2.2 Not working

I tried to reproduce the same example in this question using JSF 2.2.6 and Tomcat 7.0: JSF navigation rule doesn't work on form submit, I also read the JSF returns blank/unparsed page with plain/raw XHTML/XML/EL source instead of rendered HTML output and respected all the recommandations provided by BalusC's answer, then I also consulted this question JSF 2 with HTML pages instead of XHTML because I want to use only .html files (I know i can use .xhtml but i need to understand the reason why this is not working).
I tried to make it simple as much as possible so any one could reproduce that:
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"
version="2.5">
<servlet>
<servlet-name>facesServlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>facesServlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>faces/index.html</welcome-file>
</welcome-file-list>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.html</param-value>
</context-param>
</web-app>
faces-config.xml (Navigation rules are not needed as it's provided dynamicly):
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
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-facesconfig_2_2.xsd"
version="2.2">
<navigation-rule>
<display-name>index.html</display-name>
<from-view-id>/index.html</from-view-id>
<navigation-case>
<from-outcome>welcomePage</from-outcome>
<to-view-id>/welcome.html</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
BeanFilm.java:
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
#ManagedBean
#SessionScoped
public class BeanFilm implements Serializable {
private String recherche = new String();
public String getRecherche() {
return recherche;
}
public void setRecherche(String recherche) {
this.recherche = recherche;
}
public String doRecherche() {
return "welcomePage";
}
}
index.html:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:jsf="http://xmlns.jcp.org/jsf">
<head>
<title>Accueil</title>
</head>
<body jsf:id="body" >
<h1>Plain HTML5 with JSF</h1>
<form jsf:id="form">
<input type="text" jsf:id="recherche" jsf:value="#{beanFilm.recherche}"/>
<input type="submit" jsf:value="Submit" jsf:id="searchButton" jsf:action="#{beanFilm.doRecherche}"/>
</form>
<ui:debug/>
</body>
</html>
welcome.html:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:jsf="http://xmlns.jcp.org/jsf">
<head>
<title>Accueil</title>
</head>
<body>
<h1>Welcome</h1>
</body>
</html>
The issue:
The exact problem is that when i click on the submit the method BeanFilm#doRecherch() is never called (using a breakpoint) and i can't really understand why? another information wich may be useful, is that in HTML code source attributes are still like jsf:id="searchButton"does this mean that the HTML wasn't generated?

How to persist image in mysql database using Spring MVC and JPA

This is my imageForm.jsp:
%# page contentType="text/html;charset=UTF-8" language="java" %>
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<head>
<title></title>
</head>
<body>
<h2>Image Form</h2>
<form:form method="POST" action="/showImage">
Picture: <input type="file" name="image">
<br />
<input type="submit" value="Submit" />
</form:form>
</body>
</html>
This is showImage.jsp:
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title></title>
</head>
<body>
<h2>Show Image</h2>
<p>Profile Picture : ${image.image}</p>
</body>
</html>
And this is my controller:
package com.springapp.mvc;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;
#RequestMapping(value = "/imageForm")
public ModelAndView showImageForm(Model model) {
return new ModelAndView("imageForm", "command", new Image());
}
#Transactional
#RequestMapping(value = "/showImage")
public ModelAndView showResult(#ModelAttribute("")Image image, ModelAndView model) {
model.setViewName("showImage");
System.out.println("Transaction");
em.persist(image);
System.out.println("persisted");
model.addObject("image", image);
return model;
}
}
This is the Image.java model class:
package com.springapp.mvc;
import javax.persistence.*;
#Entity
public class Image {
#Id
private int imageID;
private byte[] image;
public int getImageID() {
return imageID;
}
public void setImageID(int imageID) {
this.imageID = imageID;
}
public byte[] getImage() {
return image;
}
public void setImage(byte[] image) {
this.image = image;
}
}
And the persistence.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="NewPersistenceUnit">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.springapp.mvc.Image</class>
<properties>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/advocatoree"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
</properties>
</persistence-unit>
</persistence>
I can choose an image from my PC and when I press the submit button, the showImage.jsp page shows me this: Profile Picture : [B#1c6a19f
The entry is persisted in database, but under the image attribute, it shows: [BLOB - 39 B]
If I click on it, I get a .bin file downloaded. I don't how I should approach this problem, can someone please help me?
the best way to do it - save the picture in any working directory using FileOutputStream, in db you can save unique picture name or path to it. Also you shall receive from the client byte[] in base64 format. The problem may be that you get an byte array represented as string (like "asd561$%#!"), then use Base64.getDecoder().decode(your_string).