Page 1 of 3 123 LastLast
Results 1 to 10 of 24

Thread: Spring for server side code + webservice

  1. #1
    Join Date
    Mar 2012
    Posts
    17

    Default 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.

  2. #2
    Join Date
    May 2011
    Location
    Madrid (Spain)
    Posts
    101

    Default

    Hi, you can use Spring REST to create a Restful webservice, example:

    Spring REST

    What have you used to access database? jdbc, jpa, hibernate, ....

  3. #3
    Join Date
    Mar 2012
    Posts
    17

    Default

    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.

  4. #4
    Join Date
    May 2011
    Location
    Madrid (Spain)
    Posts
    101

    Default

    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, ...

  5. #5
    Join Date
    Mar 2012
    Posts
    17

    Default

    can i get your mail id...
    i will attach the rar and send it to you..

  6. #6
    Join Date
    Mar 2012
    Posts
    17

    Default

    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..

  7. #7
    Join Date
    Mar 2012
    Posts
    17

    Default

    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.

  8. #8
    Join Date
    May 2011
    Location
    Madrid (Spain)
    Posts
    101

    Default

    Hi, see "Using Spring in Regular Java-Based Web Apps".

    You should use the tags to write code in the forum (see FAQ).

  9. #9
    Join Date
    Mar 2012
    Posts
    17

    Default

    thanks...

    how do i solve this error..I think it is caused due to some problem while spring configuration by tomcat itself...
    Do let me know..

    Error:
    IOException parsing XML document from class path resource [C:/Program Files/Apache Software Foundation/Tomcat 5.5/server/context.xml]; nested exception is java.io.FileNotFoundException:
    class path resource [C:/Program Files/Apache Software Foundation/Tomcat 5.5/server/context.xml] cannot be opened because it does not exist

  10. #10
    Join Date
    May 2011
    Location
    Madrid (Spain)
    Posts
    101

    Default

    Hi, your context.xml isn't in the classpath. Have you deployed it in a .war?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •