This post will explain us, how to implement palindrome logic with different ways, using java.
public class WordPalindrome {
public static void main(String[] args) {
String word="LIRIL";
boolean isPalin = isPalindromeOne(word);
if(isPalin){
System.out.println("Given word ["+word+"] is palindrome");
}else{
System.out.println("Given word ["+word+"] is not palindrome");
}
word = "Hello";
isPalin = isPalindromeOne(word);
if(isPalin){
System.out.println("Given word ["+word+"] is palindrome");
}else{
System.out.println("Given word ["+word+"] is not palindrome");
}
}
public static boolean isPalindromeOne(String word){
boolean isPalindrome = false;
//first way of checking
char[] array = word.toCharArray();
char[] newArray = new char[array.length];
for (int i = 0; i < array.length; i++) {
char c = array[array.length-i-1];
newArray[i] =(char)c;
}
if(word.equalsIgnoreCase(String.valueOf(newArray))){
isPalindrome = true;
}
//second way of checking
String reverse ="";
for (int i = 0; i < word.length(); i++) {
char c= word.charAt(i);
reverse = c+reverse;
}
if(reverse.equalsIgnoreCase(word)){
isPalindrome = true;
}
return isPalindrome;
}
}
How to work with jersy rest webservice using eclipse and weblogic server/tomcat
Step 1: Create a dynamic web project in eclipse and provide project name
Step 2: open web.xml and add below configuration code for rest webservices.
jersyRestServiceindex.htmlindex.htmindex.jspdefault.htmldefault.htmdefault.jspJersey Web Applicationcom.sun.jersey.spi.container.servlet.ServletContainerJersey Web Application/rest/*
Step 3: Create one file name called weblogic.xml and add the below code.
Step 5: Create a class name called RestServiceAPI.java add below code.
package com.siva.restservice;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
/**
*
* @author Siva
*
*/
@Path("/helloRest")
public class SimpleRestService {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getDetails(){
System.out.println("This is Get request");
return "Hello Welcome to Rest webservice";
}
@GET
@Path("/getDetail/{requestParam}")
@Produces(MediaType.TEXT_PLAIN)
public String getDetailsWithParameter(@PathParam("requestParam") String requestParam){
System.out.println("USer request paramater["+requestParam +"]");
System.out.println("This is Get request");
return "Hello Welcome to Rest webservice you have given input as -"+requestParam;
}
}
Step 6: Create one config class called- ApplicationConfig.java
Right click on src-> Webservice->RestWebservice->select Classname- which class needs to be configured in ApplicationConfig Click Next-> Finish
Run the weblogic server and open the console http://localhost:7001/console provide username and password and login the same into weblogic server admin console
Step 8: Click on the Deployments- the install the jax-rs-2.0.war, which is inside- Oracle_home/wlserver/common/deployable-libraries
If you are working on jsr311 then deploy jsr311-api-111.war which is available on same path.
For Tomcat Deployment Step 3 , 6,7,8 not required
Right click on the Project and Run AS - server- select appropriate server and deploy the same.
Step 9: Create rest client class which get the test results for rest webservice
package com.siva.restservice.test;
import javax.ws.rs.core.MediaType;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
/**
*
* @author Siva
*
*/
public class RestServiceClient {
public static void main(String[] args) {
String uri = "http://localhost:8080/jersyRestService/rest/helloRest";
try{
Client client = Client.create();
WebResource resource = client.resource(uri);
String response = resource.type(MediaType.TEXT_PLAIN).get(String.class);
System.out.println("Response from Rest webservice["+response+"]");
String request = "GetRestWithParam";
uri = uri.concat("/getDetail/").concat(request);
resource = client.resource(uri);
String response1 = resource.type(MediaType.TEXT_PLAIN).get(String.class);
System.out.println("Response from Rest webservice with parameter["+response1+"]");
}
catch(Exception ex){
ex.printStackTrace();
}
}
}
Step 10: You can see the output as below
Response from Rest webservice[Hello Welcome to Rest webservice]
Response from Rest webservice with parameter[Hello Welcome to Rest webservice you have given input as -GetRestWithParam]
Thank you very much for viewing this post. If you like, don't forget to share/comment.
Step 3: Now we need to create company.xml file under project- create one xml file add the below xml to that file.
siva34Male3500001/07/1983PresentBangaloreKarnatakaIndiaPermanentKadapaAndharaPradeshIndiaSanjay27Male5500001/07/1988PresentBangaloreKarnatakaIndiaPermanentHyderabadTelanganaIndiaMadhan34Male80000001/07/1983Present and PermanentBangaloreKarnatakaIndia
Step 4: Create pojo classes , which need to be parsed as per above xml.
i have created 3 classes - Company.java -> List of Employee.java -> List Of Address.java
Company.java - xmlroot elment is company which is there in xml.
package xmltofile;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="company")
public class Company {
private List employee = new ArrayList();
@XmlElement(name="employee")
public List getEmployee() {
return employee;
}
public void setEmployee(List employee) {
this.employee = employee;
}
}
Employee.java
package xmltofile;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
public class Employee {
private String name;
private int age;
private String gender;
private Double salary;
private String dob;
Listaddress = new ArrayList();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
@XmlElement
public ListgetAddress() {
return address;
}
public void setAddress(Listaddress) {
this.address = address;
}
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
}
Address.java
package xmltofile;
import javax.xml.bind.annotation.XmlRootElement;
public class Address {
private String city;
private String type;
private String state;
private String country;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
Step 6: Now we need to write test class to Parse the employee.xml using JAXB context and create the Flat file
and write the xml data into file at specified index.
XMLReaderAndWriteToFile.java
package xmltofile;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import com.ancientprogramming.fixedformat4j.format.FixedFormatManager;
import com.ancientprogramming.fixedformat4j.format.impl.FixedFormatManagerImpl;
import xmltofile.fixedformat.CompanyHeader;
import xmltofile.fixedformat.EmployeeHeader;
public class XMLReaderAndWriteToFile {
public static void main(String[] args) {
BufferedWriter bw = null;
try {
JAXBContext context = JAXBContext.newInstance(Company.class);
// Create Unmarshaller using JAXB context
Unmarshaller unmarshaller = context.createUnmarshaller();
System.out.println("xml file started to load...");
Company company = (Company) unmarshaller.unmarshal(new File("employee.xml"));
System.out.println("xml file loaded and parsed successfully..");
// Write to file with fixedformat
System.out.println("Flat file started to create...");
bw = new BufferedWriter(new FileWriter("fixedLength.txt", true));
System.out.println("empty flat file created...");
getCompanyHeader(bw);
System.out.println("company header written successfully to file.");
getEmployeeHeader(bw);
System.out.println("employee header written successfully to file.");
for (Employee employee : company.getEmployee()) {
EmployeeHeader employeeDetails = new EmployeeHeader();
employeeDetails.setNameHeader(employee.getName());
employeeDetails.setAgeHeader(String.valueOf(employee.getAge()));
employeeDetails.setGenderHeader(employee.getGender());
employeeDetails.setDobHeader(String.valueOf(employee.getDob()));
employeeDetails.setSalaryHeader(String.valueOf(employee.getSalary()));
String completeAddress = " ";
for (Address address : employee.getAddress()) {
String addressDetails = address.getCity() + " " + address.getState() + " " + address.getCountry()
+ " - " + address.getType();
completeAddress = completeAddress.concat(addressDetails) + " ";
}
employeeDetails.setAddressHeader(completeAddress);
FixedFormatManager manager1 = new FixedFormatManagerImpl();
String data1 = manager1.export(employeeDetails);
bw.write(data1);
bw.newLine();
}
System.out.println("Employee details written successfully to file.");
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void getEmployeeHeader(BufferedWriter bw) throws IOException {
EmployeeHeader employeeHeader = new EmployeeHeader();
employeeHeader.setNameHeader("Employee Name");
employeeHeader.setAgeHeader("Age");
employeeHeader.setGenderHeader("Gender");
employeeHeader.setDobHeader("DOB");
employeeHeader.setSalaryHeader("Salary");
employeeHeader.setAddressHeader("Address");
FixedFormatManager manager = new FixedFormatManagerImpl();
String data = manager.export(employeeHeader);
bw.write(data);
bw.newLine();
bw.newLine();
bw.newLine();
}
private static void getCompanyHeader(BufferedWriter bw) throws IOException {
CompanyHeader companyHeader = new CompanyHeader();
companyHeader.setHeader1("ABC Company");
companyHeader.setHeader2("Bangalore");
companyHeader.setHeader3("India");
FixedFormatManager manager = new FixedFormatManagerImpl();
String data = manager.export(companyHeader);
bw.write(data);
bw.newLine();
bw.newLine();
bw.newLine();
}
}
Output file will be like this.
ABC Company Bangalore
Employee Name Age Gender Salary DOB Address
siva 34 Male 35000.0 01/07/1983 Bangalore Karnataka India - Present Kadapa AndharaPradesh India - Permanent
Sanjay 27 Male 55000.0 01/07/1988 Bangalore Karnataka India - Present Hyderabad Telangana India - Permanent
Madhan 34 Male 800000.0 01/07/1983 Bangalore Karnataka India - Present and Permanent
Thank you verymuch for viewing this post. If you like this don't forget to share.