-
Mar 19th, 2012, 06:10 AM
#1
Spring for server side code + webservice
I am working on android app that connects to server to access database from mysql database.
I am done server side code using MVC architecture.
need to wrap the same using spring and then publish the same using webservice.
need help.
-
Mar 19th, 2012, 07:46 AM
#2
Hi, you can use Spring REST to create a Restful webservice, example:
Spring REST
What have you used to access database? jdbc, jpa, hibernate, ....
-
Mar 19th, 2012, 08:01 AM
#3
Thanks
to access database i have used jdbc...
My task task to convert the MVC code into one that fix into spring framework.
What i have done is at the server side on tomcat m running the servlet that responds to client request by calling the appropriate controller then app. service and then appr. value object...
i just want to wrap the same under spring framework..but facing problems..
how should i move forward ...?
Last edited by Sayaly; Mar 19th, 2012 at 08:06 AM.
-
Mar 19th, 2012, 08:14 AM
#4
OK, could you give us more details of these problems (code, logs, ...)?
This depends on the level of integration that you'd like, you can wrap only your app in a Spring Container or use spring jdbc template, spring mvc, ...
-
Mar 19th, 2012, 09:02 AM
#5
can i get your mail id...
i will attach the rar and send it to you..
-
Mar 19th, 2012, 09:57 AM
#6
my Servlet file has following code to authenticate the user:
PrintWriter out = response.getWriter();
String type = request.getParameter("type");
String jsonstring = request.getParameter("value");
HttpSession session = null;
boolean flag;
if(type.equalsIgnoreCase("login"))
{
try{
//out.print(jsonstring);
String s=new String();
loginValue user = new loginValue();
Gson g=new Gson();
user=g.fromJson(jsonstring, loginValue.class);
user=loginService.convertfromjson(jsonstring);
flag = loginService.login(user);
if (flag)
out.print(1);
else
out.print(0);
}catch (Throwable theException){
System.out.println(theException);
}
}
which then calls to loginService(business logic part)file..
loginService calls to loginDAO file(whrein i have written actual database access code)..
public static loginValue login(loginValue bean) {
Statement stmt = null;
String companyid = bean.getCompanyid();
String loginid = bean.getLoginid();
String password = bean.getPassword();
String searchQuery =
"select * from login where companyid='"
+ companyid
+ "' AND loginid='"
+ loginid
+ "' AND password='"
+ password
+ "'";
try
{
//connect to DB
currentCon = ConnectionManager.getConnection();
stmt=currentCon.createStatement();
rs = stmt.executeQuery(searchQuery);
boolean more = rs.next();
// if user does not exist set the isValid variable to false
if (!more)
{
//System.out.println("Sorry, you are not a registered user! Please sign up first");
bean.setValid(false);
}
//if user exists set the isValid variable to true
else if (more)
{
bean.setCompanyid(companyid);
bean.setLoginid(loginid);
bean.setPassword(password);
bean.setValid(true);
}
}
catch (Exception ex)
{
System.out.println("Log In failed: An Exception has occurred! " + ex);
}
return bean;
loginValue file has all the getters&setters...
This is what i done which is working fine..
Need to wrap this server side code under spring framewrok..
-
Mar 19th, 2012, 10:06 AM
#7
What i have tried is under web project i have:
loginValue.java -- getters/setters
loginDAO.java -- db access code
LoginService.java -- business logic
Again under WEB-INF i have
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app id="WebApp">
<display-name>Project</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/bean.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoade rListener</listener-class>
</listener>
</web-app>
bean.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- This bean is the parent ApplicationContext for the WebApplicationContexts defined in the WARs.
The context files listed here should contain beans that are used by all WARs,
for example Services and DAO's. -->
<bean id="ear.context" class="org.springframework.context.support.ClassPa thXmlApplicationContext">
<constructor-arg index="0">
<list>
<value>context.xml</value>
</list>
</constructor-arg>
</bean>
</beans>
context.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<import resource="definition.xml"/>
</beans>
definition.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="login" class="service.LoginServiceImpl" />
</beans>
How do i run this project?
when deployed on server..its giving me 404 error..
Or my approach to problem is wrong?
Do reply....
Last edited by Sayaly; Mar 19th, 2012 at 10:09 AM.
-
Mar 19th, 2012, 10:44 AM
#8
Hi, see "Using Spring in Regular Java-Based Web Apps".
You should use the tags to write code in the forum (see FAQ).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules